Home AMX User Forum NetLinx Studio
Options

How to convert HEX string to Float?

I have for example HEX String of float received from device
"4640E6B7"
and need to convert it to "12345.67871"

any ideas?

please help

Comments

  • Options
    Spire_JeffSpire_Jeff Posts: 1,917
    Is there always a certain number of decimal places? Do you have a portion of the control protocol that identifies what is being passed to you in HEX format? I am having some difficulty determining how the conversion can be performed without a little more information. If the first x characters are always before the decimal, and the last 8-x characters are after the decimal, it would be a lot easier.

    Jeff
  • Options
    HedbergHedberg Posts: 671
    .
    z00f wrote: »
    I have for example HEX String of float received from device
    "4640E6B7"
    and need to convert it to "12345.67871"

    any ideas?

    please help

    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).
  • Options
    z00fz00f Posts: 4
    Thank you all for answer!!!!

    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.
Sign In or Register to comment.