bigIntToBytes static method

Uint8List bigIntToBytes(
  1. BigInt? value,
  2. int length, {
  3. dynamic endianness = Endianness.Big,
})

Implementation

static Uint8List bigIntToBytes(BigInt? value, int length,
    {endianness = Endianness.Big}) {
  Uint8List? list = List<int?>.filled(0, null, growable: false) as Uint8List;
  BigInt? v = value;
  for (int i = 0; i < length; i++) {
    list.add((v! % (BigInt.from(256))).toInt());
    v ~/= (BigInt.from(256));
  }

  /// unrelated_type_equality_check checked!
  BigInt zero = 0 as BigInt;
  if (v != zero) {
    throw "Value $value is overflow from range of $length bytes";
  }
  if (endianness == Endianness.Big) {
    list = list.reversed as Uint8List?;
  }
  return Uint8List.fromList(list!);
}