fromBytes static method

BigInt fromBytes({
  1. required List<int> bytes,
  2. required int offset,
  3. required int end,
  4. Endian byteOrder = Endian.big,
  5. bool sign = false,
})

Implementation

static BigInt fromBytes(
    {required List<int> bytes,
    required int offset,
    required int end,
    Endian byteOrder = Endian.big,
    bool sign = false}) {
  BigInt result = BigInt.zero;
  if (byteOrder == Endian.little) {
    int j = 0;
    for (int i = offset; i < end; i++) {
      /// Add each byte to the result, considering its position and byte order.
      result += BigInt.from(bytes[i]) << (8 * j++);
    }
    if (result == BigInt.zero) return result;
    if (sign && (bytes[end - 1] & 0x80) != 0) {
      return result.toSigned(BigintUtils.bitlengthInBytes(result) * 8);
    }
  } else {
    int j = 0;
    for (int i = offset; i < end; i++) {
      result += BigInt.from(bytes[end - 1 - j]) << (8 * j++);
    }
    if (result == BigInt.zero) return result;
    if (sign && (bytes[offset] & 0x80) != 0) {
      return result.toSigned(BigintUtils.bitlengthInBytes(result) * 8);
    }
  }

  return result;
}