Home » Php » php – Convert IEEE 754 to decimal floating point

php – Convert IEEE 754 to decimal floating point

Posted by: admin July 12, 2020 Leave a comment


I have what I think it is an IEEE754 with single or double precision (not sure) and I’d like to convert it to decimal on PHP.

Given 4 hex value (which might be in little endian format, so basically reversed order) 4A,5B,1B,05 I need to convert it to a decimal value which I know will be very close to 4724.50073.

I’ve tried some online converters but they are far from the expected result so I’m clearly missing something.

If I echo 0x4A; I get 74 and the others are 91, 27 and 5. Not sure where to take it from here…

How to&Answers:

To convert it to float, use unpack. If the byte order is incorrect, you’ll have to reverse it yourself before unpacking. 4 bytes (32 bits) usually means it’s a float, 8 for double.

$bin = "\x4A\x5B\x1B\x05";
$a = unpack('f', strrev($bin));
echo $a[1];  // 3589825.25

I don’t see any way how this maps to 4724.50073 directly tho. Without any more test data or manufacturer’s manual this question is not fully answerable.

Speculation: judging from the size of the coordinate it’s probably some sort of projection (XYZ or mercator) which can then be converted to WGS84 or whatever you need. Unfortunately there’s no way to check since you haven’t provided both latitude and longitude.