[英]How do I unpack a double-precision value in Perl?
From this question: 从这个问题:
bytearray - Perl pack/unpack and length of binary string - Stack Overflow bytearray-Perl打包/解压缩和二进制字符串的长度
I've learned that @unparray = unpack("d "x5, $aa);
我了解到
@unparray = unpack("d "x5, $aa);
in the snippet below results with string items in the unparray
- not with double precision numbers (as I expected). 下面的代码段中的结果中的字符串项处于反
unparray
-而不是双精度数字(正如我预期的那样)。
Is it possible to somehow obtain an array of double-precision values from the $aa
bytestring in the snippet below?: 是否可以通过以下代码段中的
$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" '
Many thanks in advance for any answers, 非常感谢您的任何回答,
Cheers! 干杯!
What makes you think it's not stored as a double? 是什么让您认为它没有存储为double?
use feature qw( say );
use Config qw( %Config );
use Devel::Peek qw( Dump );
my @a = unpack "d5", pack "d5", 255,123,0,45,123;
say 0+@a; # 5
Dump $a[0]; # NOK (floating point format)
say $Config{nvsize}; # 8 byte floats on this build
Sorry, but you've misunderstood hobbs' answer to your earlier question. 抱歉,但是您误解了霍布斯对先前问题的回答。
$unparray[0]
is a double-precision floating-point value; $unparray[0]
是双精度浮点值; but length
is not like (say) C's sizeof
operator, and doesn't tell you the size of its argument. 但是
length
不像(比如说)C的sizeof
运算符,并且不会告诉您其参数的大小。 Rather, it converts its argument to a string, and then tells you the length of that string. 相反,它将参数转换为字符串,然后告诉您该字符串的长度。
For example, this: 例如,这:
my $a = 3.0 / 1.5;
print length($a), "\n";
will print this: 将打印此:
1
because it sets $a
to 2.0
, which gets stringified as 2
, which has length 1
. 因为它将
$a
设置$a
2.0
,它被字符串化为2
,其长度为1
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.