如何使用 Perl 将 48 十六进制字符串转换为字节?
我有一个十六进制字符串(长度为 48 个字符),我想使用 pack 函数,以便将其放入 Win32 字节向量中。
我怎样才能用 Perl 做到这一点?
I have a hex string (length 48 chars) that I want to convert to raw bytes with the pack function in order to put it in a Win32 vector of bytes.
How I can do this with Perl?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有关详细信息,请参阅 perlpacktut。
See perlpacktut for more information.
步骤是:
例如:
或者,更简洁地表达:
The steps are:
For example:
Or, expressed more compactly:
我有字符串:
“
61 62 63 64 65 67 69 69 6a
”,我想将其解释为十六进制值,并将其显示为 ASCII 字符(这些值应重现字符串“abcdefghij”) 。
通常,我会尝试像这样快速写一些东西:
... 然后我想知道,为什么我只得到一个字符
:)
首先,让我记下我拥有的字符串,也可以表示为它在内存中占用的字节的十六进制值:
_(NB:本质上,我想将内存中的上述字节值“转换”为输入,如果通过 hexdump 查看,则将其转换为以下字节值:< /sup>
...这就是获取输入十六进制字符串的原始值的方式。
)_
那么,这个打包/解包教程(又名系统如何存储数据) 事实证明对我来说是最有帮助的,因为它提到:
也就是说,在我的例子中,
$_
是一个单个字符串变量 - 而pack
期望输入一个列表 > 几个这样的“单个”变量(除了格式模板字符串之外);并再次输出一个“单个”变量(但是,它可能是相当大的内存块!)。就我而言,如果输出“单个”变量包含内存中每个字节中的 ASCII 代码,那么我就已经准备好了(然后我可以直接打印输出变量)。因此,为了从
$_
字符串中获取变量列表,我可以简单地在空格符号处split
它 - 但是,请注意:...必须指定要打包的元素(否则我们只能返回一个字符);那么,这些替代方案中的任何一个都有效:
I have the string:
"
61 62 63 64 65 67 69 69 6a
"which I want to interpret as hex values, and display those as ASCII chars (those values should reproduce the character string "abcdefghij").
Typically, I try to write something quick like this:
... and then I wonder, why do I get only one character back
:)
First, let me note down that the string I have, can also be represented as the hex values of bytes that it takes up in memory:
_(NB: Essentially, I want to "convert" the above byte values in memory as input, to these below ones, if viewed by hexdump:
... which is how the original values for the input hex string were obtained.
)_
Well, this Pack/Unpack Tutorial (AKA How the System Stores Data) turns out is the most helpful for me, as it mentions:
That is, in my case,
$_
is a single string variable -- whereaspack
expects as input a list of several such 'single' variables (in addition to a formatting template string); and outputs again a 'single' variable (which could, however, be a sizeable chunk of memory!). In my case, if the output 'single' variable contains the ASCII code in each byte in memory, then I'm all set (I could simply print the output variable directly, then).Thus, in order to get a list of variables from the
$_
string, I can simplysplit
it at the space sign - however, note:... that amount of elements to be
pack
ed must be specified (otherwise again we get only one character back); then, either of these alternatives work: