Perl 中的十六进制转储解析
我在文件中有一个消息的十六进制转储,我想将其放入数组中 这样我就可以对其执行解码逻辑。
我想知道这是否是解析如下消息的更简单方法。
37 39 30 35 32 34 35 34 3B 32 31 36 39 33 34 35
3B 32 31 36 39 33 34 36 00 00 01 08 40 00 00 15
6C 71 34 34 73 69 6D 31 5F 33 30 33 31 00 00 00
00 00 01 28 40 00 00 15 74 65 6C 63 6F 72 64 69
74 65 6C 63 6F 72 64 69
请注意,任何行上的数据最多可为 16 个字节。但任何行也可以包含更少的字节(最小:1)
有没有一种漂亮而优雅的方式而不是在 perl 中一次读取 2 个字符?
I have a hex dump of a message in a file which i want to get it in an array
so i can perform the decoding logic on it.
I was wondering if that was a easier way to parse a message which looks like this.
37 39 30 35 32 34 35 34 3B 32 31 36 39 33 34 35
3B 32 31 36 39 33 34 36 00 00 01 08 40 00 00 15
6C 71 34 34 73 69 6D 31 5F 33 30 33 31 00 00 00
00 00 01 28 40 00 00 15 74 65 6C 63 6F 72 64 69
74 65 6C 63 6F 72 64 69
Note that the data can be max 16 bytes on any row. But any row can contain fewer bytes too (minimum :1 )
Is there a nice and elegant way rather than to read 2 chars at a time in perl ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Perl 有一个
hex
运算符,可以为您执行解码逻辑。请记住,
split
的默认行为会在空格分隔符,例如对于输入的每一行,将其分隔为十六进制值,将值转换为数字,然后
将它们推入数组中以供稍后处理。
示例运行:
Perl has a
hex
operator that performs the decoding logic for you.Remember that the default behavior of
split
chops up a string at whitespace separators, so for exampleFor every line of the input, separate it into hex values, convert the values to numbers, and
push
them onto an array for later processing.Sample run:
我会一次读取一行,去掉空格,然后使用
pack 'H*'
来转换它。如果不知道您要应用哪种“解码逻辑”,就很难更具体。例如,以下是一个将每个字节转换为十进制的版本:示例文件的输出:
I would read a line at a time, strip the whitespace, and use
pack 'H*'
to convert it. It's hard to be more specific without knowing what kind of "decoding logic" you're trying to apply. For example, here's a version that converts each byte to decimal:Output from your sample file:
我认为一次读入两个字符是解析逻辑标记为两个字符单元的流的适当方法。
你有什么理由认为这很丑吗?
如果您尝试提取特定序列,可以使用空白不敏感的正则表达式来实现。
I think reading in two characters at a time is the appropriate way to parse a stream whose logical tokens are two-character units.
Is there some reason you think that's ugly?
If you're trying to extract a particular sequence, you could do that with whitespace-insensitive regular expressions.