簡體   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