Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Hex to float conversion

22 replies to this topic
• 9115 posts
• Last active:
• Joined: 26 Dec 2005
Dear Mr.Laszlo,

Re: Your function posted @ <!-- m -->http://www.autohotke... ... 754#105754<!-- m --> in this topic.

```MsgBox % HexToFloat(0x46EA6000)

HexToFloat(d) {
Return (1-2*(d>>31)) * (2**((d>>23 & 255)-127)) * (1+(d & 8388607)/8388608) ; 2**23
}```

I urgently require the counterpart i.e., FloatToHex().

Regards,

SandeepPR
• Guests
• Last active:
• Joined: --
do one thing store Hex value in Union and read it as char value !

[email protected]

• Moderators
• 4713 posts
• Last active: Mar 31 2012 03:17 AM
• Joined: 14 Feb 2005
This thread is earlier than the NumGet/NumPut functions implemented in AHK. Of course, you don't have unions, just allocate enough memory to hold a double (8 bytes), NumPut the floating point number there, and NumGet the bytes, or even the whole as a 64-bit integer, in hex form.

• Members
• 16 posts
• Last active: Jan 03 2009 03:21 AM
• Joined: 25 Dec 2008
i want this code IEEE754 in C language can u plz help me.
sumik kumar gupta

• Moderators
• 4713 posts
• Last active: Mar 31 2012 03:17 AM
• Joined: 14 Feb 2005
First you have to fool C to treat the bits of a float/double as a 32/64 bit integer. Cast a reference to the float to a reference to an integer, and use it in sprintf() to create a string of hex digits (format "%x").

The other direction is similar: sscanf() can convert a string of hex digits to an integer, which you can treat as a float by casting a reference to the integer to a reference to a float.

hans010
• Guests
• Last active:
• Joined: --
can somevody pls translate this:
```HexToFloat(d) {
Return (1-2*(d>>31)) * (2**((d>>23 & 255)-127)) * (1+(d & 8388607)/8388608) ; 2**23
}```
in c++

?

SirataXero
• Guests
• Last active:
• Joined: --

Futurity's original question (which I did not understand) was the following: Take the bits of a floating point number and interpret them as a hexadecimal integer. How can you convert it back to floating point. The solution is to cut the 32-bit hex number into 3 fields, as the floats are coded: bit31 = sign, bit30..bit23 = exponent+127, bit22..bit0 = mantissa, with an implicit "1." in front.

it's actually exponent - 127.

• Moderators
• 4713 posts
• Last active: Mar 31 2012 03:17 AM
• Joined: 14 Feb 2005
No, exponent+127 is correct. You need a non-negative number in the 8-bit field.