Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Hex to float conversion


  • Please log in to reply
22 replies to this topic
Futurity
  • Members
  • 21 posts
  • Last active: Feb 08 2011 02:42 PM
  • Joined: 13 Feb 2007
I have a problem here :)

i have hex number (eg 46EA6000) and i wanted convert it to float(eg 30000.0)

i found the code in c++:
UINT	nType;
int		iLen, iCnt, iDataCtr;
unsigned char	bData;

union FloatHex{
	unsigned char	uData[4];	// Four bytes to hold an IEEE 754 float
	float			fValue;		// The IEEE 754 float
}uConvert;
//Loop through the HEX chars, and convert to byte values
		//Because we read the float HEX string MSB to LSB, we must store
		//the data MSB to LSB.  So, iDataCtr was created to help readability.
		iDataCtr = (iLen / 2) - 1;			//2 text chars per HEX byte. (0-based counter)

		for (iCnt=0; iCnt<iLen; ++iCnt){
			//Read high nibble char & store			
			bData = strHex.GetAt(iCnt);		//Read high nibble char from string
			if (bData > '9')				//Convert ASCII A-F to decimal 10-15
				bData -= 7;					
			bData <<= 4;					//move to high nib & store
			uConvert.uData[iDataCtr] = bData;

			// Get low nibble (next char in string)
			++iCnt;							//inc char ptr
			bData = strHex.GetAt(iCnt);		//get next char (low nibble)
			if (bData > '9')				//Convert ASCII A-F to decimal 10-15
				bData -= 7;					
			bData -= '0';					//convert from ASCII to hex
			uConvert.uData[iDataCtr] |= bData;//merge hex value

			--iDataCtr;						//dec data union ptr
		}
		m_Float = uConvert.fValue;
but i dont know how to write it in autohotkey language :(

Can anyone help me??
Thx in advance!
Futu

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
MsgBox, % toFloat(0x400)

toFloat(Num=0) {
A_FI := A_FormatInteger
A_FF := A_FormatFloat
SetFormat, Integer, Decimal
SetFormat, Float,  15.1 ; Adjust this!
Num += 0.0
SetFormat, Integer, %A_FI%
SetFormat, Float,  %A_FF%
Return Num
}

:)

Futurity
  • Members
  • 21 posts
  • Last active: Feb 08 2011 02:42 PM
  • Joined: 13 Feb 2007
thx Skan, but that obviously doesnt work :(

all it does is converting hex number to decimal and then to float :(

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005

i have hex number (eg 46EA6000) and i wanted convert it to float(eg 30000.0)

thx Skan, but that obviously doesnt work :(
all it does is converting hex number to decimal and then to float :(

Is not it, what you wanted?

Helpy
  • Guests
  • Last active:
  • Joined: --
There is indeed a normalized way to encode floating point numbers to hex, I don't know the algorithm (but you give it).
Giving real examples for testing, with non-zero digits in decimal place, could help...

foom
  • Members
  • 386 posts
  • Last active: Jul 04 2007 04:53 PM
  • Joined: 19 Apr 2006
Edit:
Removed buggy code. Use Laszlos code below.

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
It was not clear... Anyway, theare are a couple little bugs in foom's script. Here it is fixed:
MsgBox % HexToFloat(0x46EA6000)



HexToFloat(d) {

   Loop 32

      b := (d & 1) . b, d >>= 1

   sign := 1-2*SubStr(b,1,1), expo := 0, mant := 0, e := substr(b,2,8), m := SubStr(b,10)

   Loop Parse, e

       expo := 2*expo + A_LoopField

   Loop Parse, m

       mant := 2*mant + A_LoopField

   Return sign * (2**(expo-127)) * (1+mant/2**23)

}


Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
Here is a single line function.
MsgBox % HexToFloat(0x46EA6000)

HexToFloat(d) {
      Return (1-2*(d>>31)) * (2**((d>>23 & 255)-127)) * (1+(d & 8388607)/8388608) ; 2**23
}
Edit 20070215: shift is replaced with 2**, to handle small numbers

foom
  • Members
  • 386 posts
  • Last active: Jul 04 2007 04:53 PM
  • Joined: 19 Apr 2006

It was not clear... Anyway, theare are a couple little bugs in foom's script.

Thanks for clearing that up. :)

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

a single line function


:shock:

Very nice, Sir. I was struggling to get it done, and could not. Thanks foom.

:D

Futurity
  • Members
  • 21 posts
  • Last active: Feb 08 2011 02:42 PM
  • Joined: 13 Feb 2007
Thank You all, especially Laszlo,
the script works great!

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
I fixed a bug in the single line function above.

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
That's an interesting function but I don't understand it. Care to give an explanation or links describing why integers and floats are converted differently (like Skans version)?

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Wikipedia: IEEE 754

The IEEE Standard for Binary Floating-Point Arithmetic (IEEE 754) is the most widely-used standard for floating-point computation, and is followed by many CPU and FPU implementations. The standard defines formats for representing floating-point numbers (including negative zero and denormal numbers) and special values (infinities and NaNs) together with a set of floating-point operations that operate on these values. It also specifies four rounding modes and five exceptions (including when the exceptions occur, and what happens when they do occur).


Here is an online calculator: IEEE-754 Floating-Point Conversion

:)

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
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.