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 byte order and sign.

Implementation

static BigInt fromBytes(
  List<int> bytes, {
  Endian byteOrder = Endian.big,
  bool sign = false,
}) {
  if (bytes.isEmpty) return BigInt.zero;
  final BigInt byte256 = BigInt.from(256);

  final ordered =
      byteOrder == Endian.little ? bytes.reversed.toList() : bytes;

  BigInt result = BigInt.zero;
  for (final b in ordered) {
    result = result * byte256 + BigInt.from(b);
  }

  if (sign && (ordered[0] & 0x80) != 0) {
    result -= BigInt.one << (ordered.length * 8);
  }

  return result;
}