perl - Perl 中的 'pack' 函数

标签 perl pack unpack

我的 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,最后是 74561230)。

来自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/

相关文章:

perl - Perl 中 Unix Shell 的 '-x' 选项

时间:2019-03-08 标签:c#htmlagilitypack

perl - 提示用户多个问题(是/否和文件名输入)

arrays - 为什么定义的函数不起作用?

java - Pack200/Network Transfer Format Spec SourceDebugExtension 属性的格式规范

wpf - Powershell 包 uri 对象

Maven:如何从同一输出目录中的 Artifact 中解压缩精确文件?

python - ValueError:值太多

perl - 打包/解包 - little endian - 64 位 - 问题

mysql - 如何使用 UNIX 命令行将 XML 转换为 MYSQL 插入?