fromBytes static method

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

Converts a list of bytes to a BigInt, considering the specified byte order.

Implementation

static BigInt fromBytes(
  List<int> bytes, {
  Endian byteOrder = Endian.big,
  bool sign = false,
}) {
  if (byteOrder == Endian.little) {
    bytes = bytes.reversed.toList();
  }
  BigInt result = BigInt.zero;
  for (int i = 0; i < bytes.length; i++) {
    result += BigInt.from(bytes[bytes.length - i - 1]) << (8 * i);
  }
  if (result == BigInt.zero) return result;
  if (sign && (bytes[0] & 0x80) != 0) {
    return result.toSigned(bitlengthInBytes(result) * 8);
  }

  return result;
}