HEX&十进制转换

时间:2010-12-12 21:06:37

标签: decimal hex

我有一个二进制文件,其内容的定义如下:(所有数据都存储起来 在小端(即最低有效字节))。下面的示例数字是HEX

11 63 39 46             --- Time, UTC in seconds since 1 Jan 1970.
01 00                   --- 0001 = No Fix, 0002 = SPS
97 85 ff e0 7b db 4c 40 --- Latitude, as double
a1 d5 ce 56 8d 26 28 40 --- Longitude, as double
f0 37 e1 42             --- Height in meters, as float
fe 2b f0 3a             --- Speed in km/h, as float
00 00 00 00             --- Heading (degrees ?), as float
01 00                   --- RCR, log reason. 0001=Time, 0004=Distance
59 20 6a f3 4a 26 e3 3f --- Distance in meters, as double,
2a                      --- ? Don't know
a8                      --- Checksum, xor of all bytes above not including 0x2a

来自二进制文件“HEX”中的数据如下所示

"F25D39460200269652F5032445401F4228D79BCC54C09A3A2743B4ADE73F2A83"

如果您能够支持我根据以前的说明翻译此数据行,我感谢您。

2 个答案:

答案 0 :(得分:1)

可能错了,但是这里有一个使用Ruby的镜头:

hex   = "F25D39460200269652F5032445401F4228D79BCC54C09A3A2743B4ADE73F2A83"
ints  = hex.scan(/../).map{ |s| s.to_i(16) }
raw   = ints.pack('C*')
fields = raw.unpack( 'VvEEVVVvE')

p fields
#=> [1178164722, 2, 42.2813707974677, -83.1970117467067, 1126644378, 1072147892, nil, 33578, nil]

p Time.at( fields.first )
#=> 2007-05-02 21:58:42 -0600

如果有精通#pack#unpack的人能够向我展示更好的方法来完成前三行,我将不胜感激。

答案 1 :(得分:0)

我的Cygnus Hex Editor可以加载此类文件,并使用结构模板以原生格式显示数据。

除此之外,只需要完成每个值并计算每个字节的翻译。