我的 Perl 程序:
say 'Hexadecimal notation in a Perl script';
my$str = pack 'H*', '987456123';
print unpack('H*', $str), "\n";
print unpack('h*', $str), "\n";
输出:
Hexadecimal notation in a Perl script
1. 9874561230 # Why is it showing zero here?
2. 8947652103
在我的 1
结果中,为什么它显示为零?这是什么原因造成的?
最佳答案
987456123
中有奇数个字符,而 H*
打包需要偶数,因此假设最后一对字符为零(98
,最后是 74
、56
、12
和 30
)。
来自perldoc:
Starting from the beginning of the template to pack(), each pair of characters is converted to 1 character of output. [...] If the length of the input string is not even, it behaves as if padded by a null character at the end. Similarly, "extra" nybbles are ignored during unpacking.
关于perl - Perl 中的 'pack' 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20675157/