How to convert HEX string to Float?
z00f
Posts: 4
I have for example HEX String of float received from device
"4640E6B7"
and need to convert it to "12345.67871"
any ideas?
please help
"4640E6B7"
and need to convert it to "12345.67871"
any ideas?
please help
0
Comments
Jeff
Ok. It looks to me like what you have is a 32 bit (single precision) binary representation of a floating point number. The way that a floating point number is represented as a binary number is defined by Ieee 754. Searching the forum for "binary to float" turns up two threads the first of which has some code which will convert a 32 bit (standard precision) binary representation into a floating point. It also includes a link which will lead you to:http://people.rit.edu/meseec/eecc250-winter99/IEEE-754hex32.html which will give you a very interesting result if you insert the hex string you entered:"4640E6B7"
Anyway, doing that search in the forums will lead you to a bit of code which will probably solve your problem.
Here's a link http://www.amxforums.com/showthread.php?3423-Ieee-754&highlight=binary+float to the thread with the code. It's in the second message, the one by jweather (thanks to him).
Problem solved! It is Ieee 754 and very beautiful library i found here: http://code.google.com/p/amx-netlinx-common/source/browse/trunk/math.axi
and demo code:
// b7 e6 40 46
// 12345,67871
stack_var float f_value;
stack_var long l_value;
f_value = math_build_float(hextoi('4640E6B7'));
l_value = math_float_to_bits(f_value);
send_string 0, "'VALUE(f_value): ',FORMAT('%5.5f',f_value),13,10";
send_string 0, "'HEX VALUE(l_value): ',FORMAT('%X',l_value),13,10";
It work very good!
Thank all. Topic can be closed.