arrays - 如何在perl中将bigint转换为字节数组,类似于java
问题描述
我正在尝试在 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;
推荐阅读
- security - 如何在 Yesod 应用程序中向所有端点添加标头?
- mulesoft - 在 Mulesoft 中关闭注销
- python - 如何使用不同的列用 geopandas 绘制不同的颜色属性(色调、值、饱和度)?
- c# - 有没有办法在 Azure AD 中使用用户名和密码对 Graph API 进行身份验证而无需应用程序注册
- python-3.x - 如何使用for循环遍历两个列表来创建字典
- sql - 如果当前列值为空,则从下一行获取同一列的值
- android - 为什么将 FrameLayout 与 BottomNavigation 一起使用?
- javascript - 为什么 ChartJS 的条形图不呈现特定值的条形?
- rest - Springboot REST - 公开与安全和非安全相同的服务
- scala - 在不需要的地方应用隐式转换