Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert IEEE 754 to decimal floating point

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...

like image 645
matt Avatar asked Dec 28 '15 17:12

matt


Video Answer


1 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.

like image 184
toster-cx Avatar answered Nov 06 '22 02:11

toster-cx