1

Given the following variable:

float myFloat = 0xC0451EB8;

How do I get C0451EB8 from myFloat?

Edit: Not sure why I am being down voted here with no comment. I am not asking a simple hex representation of a float or uint32. I can do this. Given the variable definition above and a [possible common] answer of:

string FloatAsHex(float myFloat) {
  return BitConverter.ToString(BitConverter.GetBytes(myFloat));
}

FloatToHex(0xC0451EB8); //will output 1F-45-40-4F, not what I expect
FloatToHex(BitConverter.ToSingle(BitConverter.GetBytes(0xC0451EB8))) //works

Although the second one obviously works, I only have access to the float variable.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
nullable
  • 2,513
  • 1
  • 29
  • 32
  • You mean asa string ? – Maciej Kozieja Feb 23 '17 at 08:05
  • Yes, as a string or byte[] - I just would like to reverse the conversion. – nullable Feb 23 '17 at 08:06
  • 1
    You can try using `.ToString("x")` but this probably would require casting to double or int – Maciej Kozieja Feb 23 '17 at 08:08
  • My apologies for the incorrect duplicate target. Your question is actually a duplicate of this one: http://stackoverflow.com/questions/74148/how-to-convert-numbers-between-hexadecimal-and-decimal-in-c. Though, given a `float` with that value, you cannot literally do what you want, because as soon as you store `0xC0451EB8` into a `float` variable, you lose precision. You can `((uint)myFloat).ToString("X")`, which will get you `"C0451F00"`, which is as close as you're going to get. – Peter Duniho Feb 23 '17 at 08:23
  • @PeterDuniho thank you, I had a feeling i was using an unsafe value and losing precision but I was tearing my hair out over the conversion. I thought I had read every possible float to hex conversion topic on SO but I guess I overlooked this one. – nullable Feb 23 '17 at 08:30

2 Answers2

1

There is no way to accomplish what you're asking. Your code is storing the integer value 0xC0451EB8, or in decimal 3225755320, in a variable of type float. The integer value has 32 bits of precision, but a float cannot represent 32 bits of numeric precision, because some of the 32 bits of the float value are committed to the exponent.

Thus, the integer value 3225755320 gets truncated to 3225755392.

You can cast the float back to uint, and then use the standard mechanism for formatting as a hexadecimal string value. E.g. ((uint)myFloat).ToString("X"). But when you do that, you'll be starting with the truncated value, and the output will be "C0451F00" (or "0xC0451F00" if you include the standard hex specifier prefix in your format string).

Once you truncate the original value, there is no way to recover it. You cannot reverse the process.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
  • Thanks Peter, unsurprisingly I didn't even try the simple cast to an unsigned integer then get a formatted string. This works for now. Thank you also for your detailed explanation, greatly appreciated. – nullable Feb 23 '17 at 08:33
0

The documentation says to use BitConverter.ToString Method (Byte[]) on msdn

This will return a string representation of the number in hex base. Example (for array of bytes representing a number):

00-01-02-04-08-10-20-40-80-FF
  • Sorry, this doesn't work. This will output 1F-40-45-1F, where I would expect it to output B8-1E-45-C0 – nullable Feb 23 '17 at 08:09