text: Answered by:
  Convert Hex to Long and Decimal
  Hi there,
  I am wondering if there is a ready method I can use to convert a Hex number (written as a string) to Long and/or Decimal. I can do it by going thru the string backwards, converting chars to God knows what, multiplying by the base(16) times position, adding the results, ect, ect, etc.
  Still I am wondering if there is a C# method that does it.
  There are methods in Microsoft.VisualBasic namespace but I could not get them in my Intellisense at all.
  Thanks.
  Question
  Answers
  All replies
  -
  Thank you,
  This is how far I got on my onw but I am getting this exception:Code Block
  privatevoid convertHexToDateTime ( )
  {
  if (this.txtBoxINPUTS.Text != String.Empty)
  {
  Int64 num = Int64.Parse ( this.txtBoxINPUTS.Text.Trim ( ), System.Globalization.NumberStyles.HexNumber );
  long numb = (long)Convert.ToDecimal ( num );
  this.rtfFrontPage.Text = Convert.ToString ( Convert.ToDateTime ( numb ) );
  }
  }
  It crashes at the last statement and the error message is:
  Invalid cast from 'Int64' to 'DateTime'???
  The value I put in is Long and I verified it. typeof (numb) = System.Long with input value = "4549b5b0"
  Thanks.
  Convert.ToDateTime(num); is invalid cast.
  the following works:
  Int64 num = Int64.Parse("FFFFF", System.Globalization.NumberStyles.HexNumber);
  long numb = Convert.ToInt64(num); //no need to convert to Decimal then to long
  DateTime dt = DateTime.FromBinary(numb);
  long IS a Int64, long is just the primitive representation for Int64 and that is not why you are having problems. You are having problems because this "long" does not represent a valid DateTime type.
  Using the FromBinary is a way around, but still probably not a valid DateTime representation and you will get (1/1/0001 00:00:00) for "FFFFF" and for most other Hex values.
  You will successfully represent a DateTime in Hex in a case like this:
  longlongint = Int64.Parse("88CA2B3C1D25971C", System.Globalization.NumberStyles.HexNumber); DateTime dt = DateTime.FromBinary(longint); MessageBox.Show(dt.ToString());
  Regards,
  Fábio