Page 1 of 1

IEEE-754 Floating-Point Conversion

Posted: Mon Sep 15, 2008 3:53 pm
by Trafalger
Hey guys, I'm pulling my hair out on this one...I've been working on this for days.

I am being presented a hex value in this case 45EF7000 and the manual of the device i'm coding for says that the hex value is in "ASCII HEX IEEE FLOAT"

Now here is where the fun starts, if you look at http://babbage.cs.qc.edu/IEEE-754/32bit.html and you enter the hex value above you will get the correct number which is 7662

I for the life of me and google cannot figure out how to do this conversion in my php code. I have tried all the examples that google has offered up and they all return crazy numbers in the millions or large negative numbers.

Could anyone help with this at all...?

Re: IEEE-754 Floating-Point Conversion

Posted: Mon Sep 15, 2008 5:26 pm
by Trafalger
Here is some more info.

I found some code online that tries to do this but it's still not working...

The machine is sending me 45836000 and if i look on the machine's display it is showing that number as 6626....

ARGH!

Re: IEEE-754 Floating-Point Conversion

Posted: Thu Jun 24, 2010 12:33 pm
by impressthenet
I'll take a stab at this and guess that you were trying to interface to a Veeder Root device. :)

I know it's been some time, but maybe this can help others.

Here's a function I'm using:

Code: Select all

function hexToIEEE754($strHex) {
    if(!strcmp($strHex,"00000000"))
	return 0;
    $binary = str_pad(base_convert($strHex, 16, 2), 32, "0", STR_PAD_LEFT);
    $sign = $binary[0];
    $exponent = bindec(substr($binary, 1, 8)) - 127;
    $mantissa = (2 << 22) + bindec(substr($binary, 9, 23));

    $floatVal = $mantissa * pow(2, $exponent - 23) * ($sign ? -1 : 1);

    return $floatVal;
}
Hope that helps!