如何在类似于Java的Perl中将bigint转换为字节数组

泰杰德

我正在尝试在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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Perl中将图像或文本文件转换为字节数组?

如何在Perl中将字节数组转换为int64

如何在Android中将位图转换为字节数组并将字节数组转换为位图?

如何在Java中将字节数组转换为十六进制字符串?

如何在Java中将字符串转换为bcd格式的字节数组?

如何在Java中将图像转换为字节数组?

如何在Java中将字节数组转换为Mat对象

如何在Java SE中将字节数组转换为Image

如何在Java中将字节数组转换为hexString?

如何在Java中将字节数组转换为String?

如何在Java中将整数值转换为'n'位数字ascii字节数组

BigInteger如何在Java中将字节数组转换为数字?

如何在Java中将带符号的字节数组转换为ASCII

如何在JNI中将Java字节数组的内容转换为C字符串?

如何在Swift中将Int转换为4个字节的字节数组?

在Java中将InputStream转换为字节数组

如何在Java中将二维布尔数组转换为一维字节数组?

如何在Go中将字节数组转换为字符串数组?

如何在C ++中将浮点数组转换为字节数组(Arduino)

如何在c#中将浮点数组转换为字节数组?

如何在Java中将具有空终止符的字节数组转换为字符串?

如何将Java字节数组转换为Scala字节数组?

如何在C ++中将64位整数转换为大端字节数组

如何在m子中将字节数组转换为多部分文件

如何在Rust中将字节数组转换为原始类型?

如何在Spring MVC中将字节数组转换为ZipOutputStream?

如何在 TwinCAT3 中将字节数组转换为各种 LREAL(double) 值

如何在PowerShell中将哈希字符串转换为字节数组?

如何在Android Studio中将imageview转换为字节数组?