IEEE-754 Floating-Point Conversion

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Trafalger
Forum Newbie
Posts: 2
Joined: Mon Sep 15, 2008 3:50 pm

IEEE-754 Floating-Point Conversion

Post 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...?
Trafalger
Forum Newbie
Posts: 2
Joined: Mon Sep 15, 2008 3:50 pm

Re: IEEE-754 Floating-Point Conversion

Post 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!
impressthenet
Forum Newbie
Posts: 2
Joined: Thu Jun 24, 2010 12:27 pm

Re: IEEE-754 Floating-Point Conversion

Post 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!
Post Reply