十六进制和十进制转换

发布于 2024-10-07 09:59:51 字数 885 浏览 2 评论 0原文

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

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

来自二进制文件“十六进制”的数据如下,

"F25D39460200269652F5032445401F4228D79BCC54C09A3A2743B4ADE73F2A83"

如果您能支持我根据之前的指令翻译此数据行,我将不胜感激。

I have a binary file , the definition of its content is as below : ( all data is stored
in little endian (ie. least significant byte first)) . The example numbers below are 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

the data from the Binary file "in HEX" is as below

"F25D39460200269652F5032445401F4228D79BCC54C09A3A2743B4ADE73F2A83"

I appreciate if you can support me to translate this data line based on the instruction before.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光沙漏 2024-10-14 09:59:51

可能是错误的,但这里有一个使用 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< code>#unpack 将向我展示一种更好的方法来完成前三行。

Probably wrong, but here's a shot at it using 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

I'd appreciate it if someone well-versed in #pack and #unpack would show me a better way to accomplish the first three lines.

呆萌少年 2024-10-14 09:59:51

我的 Cygnus Hex Editor 可以加载这样的文件,并使用结构模板,以其本机格式显示数据。

除此之外,只需处理每个值并计算出每个字节的转换即可。

My Cygnus Hex Editor could load such a file and, using structure templates, display the data in its native formats.

Beyond that, it's just a matter of doing through each value and working out the translation for each byte.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文