我正在尝试在perl中复制一些Java代码。
我需要执行的操作是将Java BigInteger转换为字节数组
Java代码:
public class Main {
public static void main(String[] args) {
BigInteger n = new BigInteger("1677259342285725925376");
System.out.println(Arrays.toString(n.toByteArray()));
}
}
输出: [90, -20, -90, 53, 78, -38, 2, -128, 0]
作为perl的新增功能,尝试搜索并从https://www.perlmonks.org/?node_id=229290获得帮助
Perl代码:
sub bigint_to_bytearray {
my $bigint = shift;
my @bytes;
while(1) {
my ($q,$r) = $bigint->brsft(8);
push(@bytes,$r+0);
last if $q == 0;
$bigint = Math::BigInt->new($q);
}
return @bytes;
}
$n = new Math::BigInt('1677259342285725925376');
my @array = bigint_to_bytearray($n);
print "\\tOUTPUT ARRAY = ", join ', ', @array, "\n";
我遇到错误
Use of uninitialized value $r in addition (+) at path/test.pl line 11.
Use of uninitialized value $r in addition (+) at path/test.pl line 11.
Use of uninitialized value $r in addition (+) at path/test.pl line 11.
Use of uninitialized value $r in addition (+) at path/test.pl line 11.
Use of uninitialized value $r in addition (+) at path/test.pl line 11.
Use of uninitialized value $r in addition (+) at path/test.pl line 11.
Use of uninitialized value $r in addition (+) at path/test.pl line 11.
Use of uninitialized value $r in addition (+) at path/test.pl line 11.
这不支持负数。
sub bigint_to_bytearray {
my $bigint = shift;
die "Negative numbers not supported" if $bigint->is_neg;
my @bytes = unpack('c*', $bigint->to_bytes);
unshift @bytes, 0x00 if $bytes[0] < 0; # Add sign bit if necessary.
return @bytes;
}
上面的内容要求使用Math :: BigInt的较新版本。以下代码效率较低,但可在较旧的版本上运行:
sub bigint_to_bytearray {
my $bigint = shift;
die "Negative numbers not supported" if $bigint->is_neg;
my @bytes = unpack('c*', pack('H*', substr($bigint->as_hex, 2)));
unshift @bytes, 0x00 if $bytes[0] < 0; # Add sign bit if necessary.
return @bytes;
}
my @bytes = unpack('c*', $bigint->to_bytes);
可以替换为
my @bytes =
map { $_ >= 0x80 ? 0x100 - $_ : $_ }
map ord,
split //,
$bigint->to_bytes;
和
my @bytes = unpack('c*', pack('H*', substr($bigint->as_hex, 2)));
可以替换为
my $hex = substr($bigint->as_hex, 2);
$hex = "0$hex" if length($hex) % 2;
my @bytes =
map { $_ >= 0x80 ? 0x100 - $_ : $_ }
map hex,
$hex =~ /../g;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句