大数的十进制和二进制转换

Decimal and binary conversion with big numbers

我必须将 Perl 中的大数字从十进制转换为二进制,反之亦然。

该长度的示例数:

Dec: 76982379919017706648824420266
Bin: 111110001011111001010101000010011001000010101111001110000000000000000000000000000000000000000000

我找到了两个函数:

sub dec2bin {
    my $str = unpack("B32", pack("N", shift));
    $str =~ s/^0+(?=\d)//;   # otherwise you'll get leading zeros
    return $str;
}
sub bin2dec {
    return unpack("N", pack("B32", substr("0" x 32 . shift, -32)));
}

但是,他们两个似乎都停止使用大数字。

Output of
bin2dec(111110001011111001010101000010011001000010101111001110000000000000000000000000000000000000000000)
is 1543163 
and output of
dec2bin(76982379919017706422040262422)
is 11111111111111111111111111111111

有没有正确的方法来处理这么大的数字?

Perl 提供 built-in bignum facilities。使用 use bignum; 打开它们。您的转换函数如下所示:

use bignum;
my ($b_orig, $d_orig, $b, $d);

$d_orig = 76982379919017706648824420266;
$b_orig = '111110001011111001010101000010011001000010101111001110000000000000000000000000000000000000000000';

print ("dec($b_orig) [orig] = $d_orig;\n");
print ("dec($b_orig) [comp] = " . Math::BigInt->from_bin($b_orig) . ";\n");
print ("bin($d_orig) [orig] = $b_orig;\n");
print ("bin($d_orig) [comp] = ".substr(Math::BigInt->new($d_orig)->as_bin(), 2).";\n");

警告

您提供的二进制数与十进制数不存在对应关系。我没有检查这是否是 bigint 库的缺陷。

您可以使用 Math::BigInt。请注意,这些函数的输入应该是字符串。

use Math::BigInt;

sub bin2dec {
  my $bin = shift;
  return Math::BigInt->new("0b$bin");
}

sub dec2bin {
  my $dec = shift;
  my $i = Math::BigInt->new($dec);
  return substr($i->as_bin(), 2);
}

print "Dec: " . bin2dec("111110001011111001010101000010011001000010101111001110000000000000000000000000000000000000000000") . "\n";
print "Bin: " . dec2bin("76982379919017706648824420266") . "\n";

输出为:

Dec: 76982379919017710405206147072
Bin: 111110001011111001010101000010011001000010101111001101001001010101100110001100111001011110101010

Perl 的 bigint 提供对大整数的透明支持:

perl -Mbigint -E 'say oct "0b111110001011111001010101000010011001000010101111001110000000000000000000000000000000000000000000"'
76982379919017710405206147072

您不需要编写自己的转换例程。 oct 将为您转换。