I have a string
6e6de179a94a4b406efab31f29d216c0e2ff0000
which I am told is defined as uint8_t and unpacks as latitude [8], longitude [8] and altitude [4].
I think this hex should decode to 54.58335
-5.70542
-15
.
How could I decode such a string using Ruby?
Fun question :)
The string is 40 hex chars long, so it represents 20 bytes.
Looking at your link, that would be 8 bytes for latitude, 8 bytes for longitude, and 4 bytes for the altitude. To be honest, I just tried the different parameters for pack
and unpack
until it looked like the desired floats:
hex = "6e6de179a94a4b406efab31f29d216c0e2ff0000"
lat_hex, lon_hex, alt_hex = hex[0,16], hex[16, 16], hex[32, 8]
lat_int, lon_int, alt_int = lat_hex.to_i(16), lon_hex.to_i(16), alt_hex.to_i(16)
p [lat_int].pack('q>').unpack('D').first
# 54.583297
p [lon_int].pack('q>').unpack('D').first
# -5.705235
Here's a shorter way:
hex.scan(/../).map{ |x| x.hex }.pack('C*').unpack('DDL')
# => [54.583297, -5.705235, 65506]
I'm not sure about the altitude. Since it's stored as an integer, it's probably modified later with some linear function to fit the GPS precision and the usual altitude range.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With