8

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

jnersn
  • 384
  • 1
  • 3
  • 14
matt
  • 2,312
  • 5
  • 34
  • 57
  • what is the exact data you have? – cmorrissey Dec 28 '15 at 17:44
  • @cmorrissey They are supposed to be GPS coordinates, the description in the docs says `All floating point values are transmitted in IEEE754 single or double precision` then I get these exact values `4A,5B,1B,05` which I know it is `4724.50073`, just not sure how to get there. – matt Dec 28 '15 at 17:46
  • 1
    Are you sure about the correspondence of the hex values and the decimal value you give? I see that in the binary representation of 4724.50073 there are three consecutive 1 digits, no matter whether you take the decimals after the point or not. And there is no such sequence of binary digits in the hex numbers, no matter how you order them. – trincot Dec 28 '15 at 19:39
  • 1
    Could you provide us with the specific documentation of the message you are processing? – trincot Dec 29 '15 at 09:36
  • If it's little endian, I'd have to wonder why the exponent is so small. If it's big endian, a quick visit to http://www.binaryconvert.com/convert_float.html reveals that it's not what you expect. Also, since these are coordinates, you might consider that they have different representations (dº m' s" for instance) – Opux Dec 30 '15 at 19:51
  • Can you explain what the purpose of converting GPS coordinates to decimal is for, this may give us an idea of what you are trying to achieve which in turn could help with identifying an alternative option for you which you may not have considered, in general trying to convert a high precision coordinate in IEEE754 into a lower precision decimal value would achieve nothing other than rendering your coordinate value less accurate, and also a strong likelihood of preventing you from being able to use that converted value in later mapping tasks as the mapping would call for the IEEE754 value. – Chris Rutherfurd Feb 10 '16 at 05:05
  • I've no problem in GPS coordinates being transmitted in digital form, as latitude longitude values are stored in a decimal form and used by mapping software. The question is how the original value is being encoded such that it represents the hexadecimal values quoted. – Jim Grant Mar 17 '16 at 10:45
  • I'm assuming that the value 4724.50073 is actually a GPS coordinate in D M.S format as opposed to anything else since 4724 is too big a number to represent a GPS coordinate on its own. Possibly 47 degrees 24.50073'. More information is required either more samples to compare or the precise location the 4724.50073 represents. – Jim Grant Mar 17 '16 at 10:53
  • Is the original hexadecimal value coming from a GPS device/chip? Just that I've known some units to use two's compliment as well in order to encode values. When I get home I will check against my code to see if your value represents something meaningful. – Jim Grant Mar 17 '16 at 10:55

1 Answers1

2

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.

toster-cx
  • 2,287
  • 1
  • 26
  • 32