Perl打包/解包和二进制字符串的长度 [英] Perl pack/unpack and length of binary string

查看:103
本文介绍了Perl打包/解包和二进制字符串的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下简短示例:

$a = pack("d",255);
print length($a)."\n";
# Prints 8

$aa = pack("ddddd", 255,123,0,45,123);
print length($aa)."\n";
# Prints 40

@unparray = unpack("d "x5, $aa);
print scalar(@unparray)."\n";
# Prints 5

print length($unparray[0])."\n"
# Prints 3

printf "%d\n", $unparray[0] '
# Prints 255

# As a one-liner:
# perl -e '$a = pack("d",255); print length($a)."\n"; $aa = pack("dd", 255,123,0,45,123); print length($aa)."\n"; @unparray = unpack("d "x5, $aa); print scalar(@unparray)."\n"; print length($unparray[0])."\n"; printf "%d\n", $unparray[0] '

现在,我希望双精度浮点数为八个字节,因此第一个length($a)是正确的.但是为什么解压缩(length($unparray[0]))后的长度报告为3-当我尝试以完全相同的方式返回(双精度,即八个字节)时-项的值(255)正确保存了?

Now, I'd expect a double-precision float to be eight bytes, so the first length($a) is correct. But why is the length after the unpack (length($unparray[0])) reporting 3 - when I'm trying to go back the exact same way (double-precision, i.e. eight bytes) - and the value of the item (255) is correctly preserved?

推荐答案

通过解压缩打包的内容,您已经获得了原始值,并且第一个值是255.255的字符串化是"255",即长3个字符,这就是length告诉您的内容.

By unpacking what you packed, you've gotten back the original values, and the first value is 255. The stringification of 255 is "255", which is 3 characters long, and that's what length tells you.

这篇关于Perl打包/解包和二进制字符串的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆