perl的.将64位二进制数转换为十进制数

Nik*_*ita 2 perl unpack pack

我有一个包含64个二进制符号的字符串.

我需要将其转换为十进制数.我怎么能在perl中做到这一点?

sub bin2dec {
    return unpack("N", pack("B64", substr("0" x 64 . shift, -64)));
}
Run Code Online (Sandbox Code Playgroud)

不起作用.它只转换前32位.

ike*_*ami 8

文档中,

N  An unsigned long (32-bit) in "network" (big-endian) order.
Run Code Online (Sandbox Code Playgroud)

64位的等价物将是" Q>".

q  A signed quad (64-bit) value.
Q  An unsigned quad value.
  (Quads are available only if your system supports 64-bit
  integer values _and_ if Perl has been compiled to support
  those. Raises an exception otherwise.)

>   sSiIlLqQ   Force big-endian byte-order on the type.
    jJfFdDpP   (The "big end" touches the construct.)
Run Code Online (Sandbox Code Playgroud)

所以你可以使用以下内容:

unpack("Q>", pack("B64", substr("0" x 64 . shift, -64)))
Run Code Online (Sandbox Code Playgroud)

也就是说,上述情况不必要地复杂化.那些编码的人可能不知道oct解析二进制数的能力,因为上面的内容可以减少到

oct("0b" . shift)
Run Code Online (Sandbox Code Playgroud)

但是,如果你没有64位构建的Perl,你会怎么做?您需要使用某种重载数学运算的对象.你可以使用Math :: BigInt,但我怀疑它不会像Math :: Int64那么快.

use Math::Int64 qw( string_to_int64 );
string_to_int64(shift, 2)
Run Code Online (Sandbox Code Playgroud)

例如,

$ perl -MMath::Int64=string_to_int64 -E'say string_to_int64(shift, 2);' \
   100000000000000000000000000000000
4294967296
Run Code Online (Sandbox Code Playgroud)