How to Convert a Float into Hex

Converting float to hex format

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.

Signed float to hexadecimal number

This is simply the Q16.16 fixed-point format. To convert a floating-point number to this format, simply multiply it by 216 (in Python, 1<<16 or 65536) and convert the product to an integer:

y = int(x * (1<<16))

To show its 32-bit two’s complement representation, add 232 if it is negative and then convert it to hexadecimal:

y = hex(y + (1<<32 if y < 0 else 0))

For example, the following prints “0xfff31a35”:

#!/usr/bin/python

x=-12.897631
y = int(x * (1<<16))
y = hex(y + (1<<32 if y < 0 else 0))
print(y)

This conversion truncates. If you want rounding, you can add .5 inside the int or you can add additional code for other types of rounding. You may also want to add code to handle overflows.

How do I convert a floating point to hexadecimal in VB.NET?

Ok, so I found how to do it:

Dim var As Single = Single.Parse("-1580.719")
Dim varArray() As Byte = BitConverter.GetBytes(var)
Array.Reverse(varArray)
Dim result As String = BitConverter.ToString(varArray).Replace("-", "")

Value of result is:

C4C59702

Converting float to hex results in more digits than expected in C#?

EDIT: Oh dear oh dear, I completely missed this, which is the short answer:

Use BitConverter.GetBytes and pass it a float, as shown here.

The long answer:

BitConverter doesn't support single precision floats, just doubles. You'll have to create a C# "union", like so:

[StructLayout(LayoutKind.Explicit)]
class Floater
{
[FieldOffset(0)]
public float theFloat;
[FieldOffset(0)]
public int theInt;
}

Put your float in theFloat and look at theInt

Arduino convert float to hex IEEE754 Single precision 32-bit

f is already stored in binary. reinterpret_cast is generally a code smell issue, but its valid use is to view the byte representation of variables.


void FloatToHex(float f, byte* hex){
byte* f_byte = reinterpret_cast<byte*>(&f);
memcpy(hex, f_byte, 4);
}

void setup() {
float f = 3.10;
byte hex[4] = {0};

FloatToHex(f, hex);
//... do stuff with hex now...
}

void loop() {

}

Convert float to 32bit hex string in JavaScript

You could take the TypedArray object with an ArrayBuffer and DataView.

Then set the value as float 32 and read the view as unsigned integer 8 bit for the values.