如何在 Perl 中解压双精度值?
来自这个问题:
bytearray - Perl pack/unpack 和二进制字符串的长度 - Stack Overflow
我了解到,下面代码片段中的 @unparray = unpack("d "x5, $aa);
会在 unparray
中产生字符串项- 不是具有双精度数字(如我所料)。
是否有可能以某种方式从下面代码片段中的 $aa
字节字符串中获取双精度值数组?:
$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
# one liner:
# perl -e '$a = pack("d",255); print length($a)."\n"; $aa = pack("ddddd", 255,123,0,45,123); print length($aa)."\n"; @unparray = unpack("d "x5, $aa); print scalar(@unparray)."\n"; print length($unparray[0])."\n" '
提前非常感谢您的回答,
干杯!
From this question:
bytearray - Perl pack/unpack and length of binary string - Stack Overflow
I've learned that @unparray = unpack("d "x5, $aa);
in the snippet below results with string items in the unparray
- not with double precision numbers (as I expected).
Is it possible to somehow obtain an array of double-precision values from the $aa
bytestring in the snippet below?:
$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
# one liner:
# perl -e '$a = pack("d",255); print length($a)."\n"; $aa = pack("ddddd", 255,123,0,45,123); print length($aa)."\n"; @unparray = unpack("d "x5, $aa); print scalar(@unparray)."\n"; print length($unparray[0])."\n" '
Many thanks in advance for any answers,
Cheers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是什么让你认为它没有存储为双精度?
What makes you think it's not stored as a double?
抱歉,您误解了霍布斯对您之前问题的回答。
$unparray[0]
是双精度浮点值;但length
与 C 的sizeof
运算符不同,并且不会告诉您其参数的大小。相反,它将其参数转换为字符串,然后告诉您该字符串的长度。例如,这个:
将打印这个:
因为它将
$a
设置为2.0
,它被字符串化为2
,长度为1.
.Sorry, but you've misunderstood hobbs' answer to your earlier question.
$unparray[0]
is a double-precision floating-point value; butlength
is not like (say) C'ssizeof
operator, and doesn't tell you the size of its argument. Rather, it converts its argument to a string, and then tells you the length of that string.For example, this:
will print this:
because it sets
$a
to2.0
, which gets stringified as2
, which has length1
.