Ruby 解包为多个 8 位地址的十六进制表示?
我正在阅读一个如下所示的文件:
0025be60 24 b3 10 80 00 b4 10 80 a4 b4 10 80 08 b5 10 80 |$...............|
0025be70 94 b5 10 80 9c b7 10 80 40 b9 10 80 e4 b9 10 80 |........@.......|
0025be80 e0 bf 10 80 94 c0 10 80 f4 cc 10 80 54 cd 10 80 |............T...|
0025be90 44 d9 10 80 88 d9 10 80 30 da 10 80 88 db 10 80 |D.......0.......|
0025bea0 44 dc 10 80 d0 e3 10 80 6c e6 10 80 d0 e8 10 80 |D.......l.......|
但很难将其转换为十六进制变量数组。我想要以下内容:
x = [0x24b31080, 0x00b41080, 0xa4b41080, 0x08b51080 ...
但我遇到了问题。我认为 file_contents.unpack("H8*) 会起作用,但这只返回第一个值......那里有解包专家吗?
I am reading a file which looks like:
0025be60 24 b3 10 80 00 b4 10 80 a4 b4 10 80 08 b5 10 80 |$...............|
0025be70 94 b5 10 80 9c b7 10 80 40 b9 10 80 e4 b9 10 80 |........@.......|
0025be80 e0 bf 10 80 94 c0 10 80 f4 cc 10 80 54 cd 10 80 |............T...|
0025be90 44 d9 10 80 88 d9 10 80 30 da 10 80 88 db 10 80 |D.......0.......|
0025bea0 44 dc 10 80 d0 e3 10 80 6c e6 10 80 d0 e8 10 80 |D.......l.......|
but am having a hard time converting it into an array of hex vaiables. I would like the following:
x = [0x24b31080, 0x00b41080, 0xa4b41080, 0x08b51080 ...
but am having a problem. I thought file_contents.unpack("H8*) would work, but that only returns the first value... Any unpack experts out there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须对数据进行更多转换。一种方法是在输入流上使用
gsub
来删除空格。此处对此进行了说明:如果您想忽略该行的一部分,您始终可以将其范围调整得更好一些:
这将从字符串中的偏移量 10 开始获取 48 个字符,然后将其解包。最后的
flatten
会将两层数组结构转换为单层。You'll have to transform the data a bit more. One way to do this is to use
gsub
on your input stream to remove spaces. This is illustrated here:If you want to ignore part of the line, you can always scope it a bit better:
This is taking 48 characters starting at offset 10 in the string and unpacking those. The
flatten
at the end will convert the two-level array structure into a single level.