Search code examples
gps

How to convert GPS Longitude and latitude from hex


I am trying to convert GPS data from GPS tracking device. The company provided protocol manual, but it's not clear. Most of the data I was able to decoding from the packets I received from the device. The communication is over TCP/IP. I am having a problem decoding the hex value of the longitude and latitude. Here is an example from the manual:

Example: 22º32.7658’=(22X60+32.7658)X3000=40582974, then converted into a hexadecimal number 40582974(Decimal)= 26B3F3E(Hexadecimal) at last the value is 0x02 0x6B 0x3F 0x3E.

I would like to know how to reverse from the hexadecimal to longitude and latitude. The device will send 26B3F3E. I want to know the process of getting 22º32.7658.

This protocol applies to GT06 and Heacent 908.


Solution

    1. Store all four values in unsigned 32-bit variables.
      v1 = 0x02, v2 = 0x6b, v3 = 0x3f, v4 = 0x3e.

    2. Compute (v4 << 48) | (v3 << 32) | (v2 << 16) | v1 this will yield a variable holding the value 40582974 decimal.

    3. Convert this to a float and divide it by 30,000.0 (your 3,000 was an error), this will give you 1,352.765

    4. Chop to integer and divide by 60. This will give you the 22.

    5. Multiply the number you got in step 4 by 60 and subtract it from the number you got in step 3. This will give you 1352.765 - 22*60 or 32.765.

    There's your answer 22, 32.765.