Daniel
Daniel

Reputation: 105

Python program to convert Hexadecimal to IEEE-754 Floating Point with Single precision

I'm writing a Python program to decode Base64(Raw Data) output from my sensors to find it's latitude and longitude.

After checking the documents (attached below) that came with the sensor to decode the values, I'm still unsure on how to achieve it. I found a partial solution to convert it below but not sure how to achieve the final output.

So for example, if the Hexa value is B4 39 F5 42, my latitude output should be 1.3768.

enter image description here

import binascii
import struct


data = 'F9 7B 9C 45'
fdata = struct.unpack('<f', binascii.unhexlify(data.replace(' ', '')))[0]
print(fdata)

output:

5007.49658203125

Upvotes: 2

Views: 1682

Answers (1)

Selcuk
Selcuk

Reputation: 59228

You are almost there. You should now convert the floating point number to degrees as specified in the docs you attached:

deg = fdata // 100 + (fdata % 100) / 60

The result would be 50.124943033854166 for 'F9 7B 9C 45' and 1.3768783569335938 for 'B4 39 F5 42'.

Upvotes: 2

Related Questions