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,
}) {
  final int length = end - offset;
  if (length <= 0) return BigInt.zero;

  BigInt result = BigInt.zero;
  bool negative;

  if (byteOrder == Endian.little) {
    // Most significant byte is at `end - 1`; walk backwards so each
    // subsequent byte is shifted further left.
    for (int i = end - 1; i >= offset; i--) {
      result = (result << 8) | BigInt.from(bytes[i] & 0xff);
    }
    negative = sign && (bytes[end - 1] & 0x80) != 0;
  } else {
    // Most significant byte is at `offset`.
    for (int i = offset; i < end; i++) {
      result = (result << 8) | BigInt.from(bytes[i] & 0xff);
    }
    negative = sign && (bytes[offset] & 0x80) != 0;
  }

  if (negative) {
    result -= BigInt.one << (length * 8);
  }

  return result;
}