在 Perl 中,pack
和 unpack
有两个用于将字节与十六进制相互转换的模板:
h
十六进制字符串(低位 nybble 在前)。
H
一个十六进制字符串(高半字节在前)。
最好用一个例子来说明这一点:
use 5.010; # so I can use say
my $buf = "\x12\x34\x56\x78";
say unpack('H*', $buf); # prints 12345678
say unpack('h*', $buf); # prints 21436587
如您所见,当人们考虑将字节与十六进制相互转换时,通常会想到 H
。那么 h
的用途是什么?拉里一定认为有人可能会使用它,否则他就不会费心将它包括在内。
您能否给出一个实际的示例,您实际上希望在 pack
或 中使用 h
而不是 H
解压
?我正在寻找一个具体的例子;如果您知道有一台像这样组织字节的机器,它是什么,您可以链接到它的一些文档吗?
我可以想到你可以使用 h
的例子,例如当你并不真正关心格式是什么时序列化一些数据,只要你可以读回它,但是 H
对此也同样有用。我正在寻找一个示例,其中 h
比 H
更有用。
In Perl, pack
and unpack
have two templates for converting bytes to/from hex:
h
A hex string (low nybble first).
H
A hex string (high nybble first).
This is best clarified with an example:
use 5.010; # so I can use say
my $buf = "\x12\x34\x56\x78";
say unpack('H*', $buf); # prints 12345678
say unpack('h*', $buf); # prints 21436587
As you can see, H
is what people generally mean when they think about converting bytes to/from hexadecimal. So what's the purpose of h
? Larry must have thought someone might use it, or he wouldn't have bothered to include it.
Can you give a real-world example where you'd actually want to use h
instead of H
with pack
or unpack
? I'm looking for a specific example; if you know of a machine that organized its bytes like that, what was it, and can you link to some documentation on it?
I can think of examples where you could use h
, such as serializing some data when you don't really care what the format is, as long as you can read it back, but H
would be just as useful for that. I'm looking for an example where h
is more useful than H
.
发布评论
评论(3)
回想一下 MS-DOS 的糟糕日子,某些操作系统功能是通过设置来控制的寄存器上的高半字节和低半字节并执行中断 xx。例如,Int 21 访问许多文件函数。您可以将高半字节设置为驱动器编号 - 谁将拥有超过 15 个驱动器?低半字节作为该驱动器上请求的功能,等等。
此处 是一些旧的 CPAN 代码,它使用您所描述的 pack 来设置寄存器以执行 MS-DOS 系统调用。
布莱奇!!!我一点也不怀念 MS-DOS...
--编辑
这里是具体的源代码:下载 Perl 5.00402 for DOS 此处,解压缩,
在文件 Opcode.pm 和 Opcode.pl 中,您可以在此处看到
unpack("h*",$_[0]);
的使用:我没有完全遵循代码,但我怀疑这是从 MS-DOS 系统调用中恢复信息...
在 perlport 对于 Perl 5.8-8,您可以对目标的字节顺序进行以下建议测试:
看起来
unpack("h*",...)
比pack("h*",...)
使用得更频繁。我确实注意到return qq'unpack("F", pack("h*", "$hex"))';
用于Deparse.pm
和 < code>IO-Compress 在 Perl 5.12 中使用pack("*h",...)
如果您想要更多示例,这里有一个 Google 代码搜索列表。您可以看到
pack|unpack("h*"...)
相当罕见,主要与确定平台字节序有关...Recall in the bad 'ole days of MS-DOS that certain OS functions were controlled by setting high nibble and low nibbles on a register and performing an Interupt xx. For example, Int 21 accessed many file functions. You would set the high nibble as the drive number -- who will have more than 15 drives?? The low nibble as the requested function on that drive, etc.
Here is some old CPAN code that uses pack as you describe to set the registers to perform an MS-DOS system call.
Blech!!! I don't miss MS-DOS at all...
--Edit
Here is specific source code: Download Perl 5.00402 for DOS HERE, unzip,
In file Opcode.pm and Opcode.pl you see the use of
unpack("h*",$_[0]);
here:I did not follow the code all the way through, but my suspicion is this is to recover info from an MS-DOS system call...
In perlport for Perl 5.8-8, you have these suggested tests for endianess of the target:
It seems that
unpack("h*",...)
is used more often thanpack("h*",...)
. I did note thatreturn qq'unpack("F", pack("h*", "$hex"))';
is used inDeparse.pm
andIO-Compress
usespack("*h",...)
in Perl 5.12If you want further examples, here is a Google Code Search list. You can see
pack|unpack("h*"...)
is fairly rare and mostly to do with determining platform endianess...我想这在向具有不同字节序的机器传输数据或从具有不同字节序的机器读取数据时很有用。如果某个进程希望以通常在内存中表示的方式接收数据,那么您最好以这种方式发送数据。
I imagine this being useful when transfering data to or reading data from a machine with different endianess. If some process expects to receive data the way it would normally represent it in memory, then you better send your data just that way.
两者之间的区别仅与您使用的是大端数据还是小端数据有关。有时您无法控制数据的源或目的地,因此要打包的
H
和h
标志可以为您提供选择。V
和N
的存在也是出于同样的原因。The distinction between the two just has to do with whether you are working with big-endian or little-endian data. Sometimes you have no control over the source or destination of your data, so the
H
andh
flags to pack are there to give you the option.V
andN
are there for the same reason.