繁体   English   中英

将字符串的字节数组转换为大 integer 并以任何语言执行功能

[英]convert strings's byte array to big integer and perform functions in any language

我想将 String 的字节数组转换为 BigInteger,反之亦然。

我可以专门针对某些语言执行此操作。 例如:-

在 java 中: new BigInteger("ab".getBytes())

并在 dart: _convertBytesToBigInt(Uint8List.fromList(Utf8Encoder().convert('ab')))

其中_convertBytesToBigInt()方法来自 dartlang sdk 的 github 问题讨论( 此处此处)。

我尝试处理从 dart 到 java 的相同代码:

    BigInteger _convertBytesToBigInt(byte[] bytes) {
        BigInteger result = BigInteger.ZERO;
        for (byte z : bytes) {
// reading in big-endian, so we essentially concat the new byte to the end
            result = (result.shiftLeft(8)).or(BigInteger.valueOf(z));
        }
        return result;
    }

当我尝试在 java 中执行简单的乘法/除法时,这不起作用(但它在 flutter 中有效)

// in java:

byte[] x = writeBigInt(_convertBytesToBigInt(writeBigInt(_convertBytesToBigInt("ab".getBytes()).multiply(BigInteger.TEN))).divide(BigInteger.TEN));
//returns [-25, -6] but it should return [97,98]
//in dart:

Uint8List y = _writeBigInt(_convertBytesToBigInt(_writeBigInt(
          _convertBytesToBigInt(
                  Uint8List.fromList(const Utf8Encoder().convert('ab'))) *
              BigInt.two)) ~/
      BigInt.two);

//it returns [97,98] which is expected result.

我尝试复制 Java 的 BigInteger class 和它的toByteArray()方法,但同样,它不能正确地与 dart 一起使用。

如果我尝试不进行乘法/除法,这些方法可以正常工作,所以唯一可能的情况是,如果我没记错的话,它们都会以不同的方式进行乘法/除法。

有什么解决方案吗? 这样我就可以用任何语言将字节 arr 转换为 BigInteger(特别是流行的语言,如 Java、Dart/flutter、Rust、ZA7F5F35426B927411FC9231B563827 等)

笔记:

以下是我分别在 dart 和 java 中使用的方法。

BigInt _convertBytesToBigInt(Uint8List bytes) {
  BigInt result = BigInt.zero;

  for (final byte in bytes) {
// reading in big-endian, so we essentially concat the new byte to the end
    result = (result << 8) | BigInt.from(byte);
  }
  return result;
}

Uint8List _writeBigInt(BigInt number) {
  // Not handling negative numbers. Decide how you want to do that.
  int bytes = (number.bitLength + 7) >> 3;
  var b256 = BigInt.from(256);
  var result = Uint8List(bytes);
  for (int i = 0; i < bytes; i++) {
    result[i] = number.remainder(b256).toInt();
    number = number >> 8;
  }
  return result;
}
BigInteger _convertBytesToBigInt(byte[] bytes) {
        BigInteger result = BigInteger.ZERO;
        for (byte z : bytes) {
// reading in big-endian, so we essentially concat the new byte to the end
            result = (result.shiftLeft(8)).or(BigInteger.valueOf(z));
        }
        return result;
    }

static byte[] writeBigInt(BigInteger number) {
        // Not handling negative numbers. Decide how you want to do that.
        int bytes = (number.bitLength() + 7) >> 3;
        BigInteger b256 = BigInteger.valueOf(256);
        byte[] result = new byte[bytes];
        for (int i = 0; i < bytes; i++) {
            result[i] = (byte) Integer.parseInt(number.remainder(b256).toString());
            number = number.shiftRight(8);
        }
        return result;
    }

在 Java 中,一个字节是有符号的 8 位值(范围从 -128 到 127)。 要获得无符号值,您可以将其与 255 逐位与。尝试

BigInteger _convertBytesToBigInt(byte[] bytes) {
        BigInteger result = BigInteger.ZERO;
        for (byte z : bytes) {
            result = (result.shiftLeft(8)).or(BigInteger.valueOf(z & 0xff));
        }
        return result;
    }

关于writeBigInt的旁注:只需使用Number#byteValue()而不是toString()parseInt()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM