How to convert hex to uint8_t using ruby

I have a line

6e6de179a94a4b406efab31f29d216c0e2ff0000

which, I am told, is defined as uint8_t and unpacked as latitude [8], longitude [8] and height [4].

I think this hex should decode before 54.58335 -5.70542 -15.

How can I decode such a string using Ruby?

+4
source share
1 answer

Funny question :)

The string contains 40 hexadecimal characters, so it represents 20 bytes.

, 8 , 8 4 . , pack unpack, :

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

:

hex.scan(/../).map{ |x| x.hex }.pack('C*').unpack('DDL')
# => [54.583297, -5.705235, 65506]

. , , , , GPS .

+3

Source: https://habr.com/ru/post/1676514/


All Articles