convertBigIntToByteList function

Uint8List convertBigIntToByteList(
  1. BigInt number
)

Implementation

Uint8List convertBigIntToByteList(BigInt number) {
  // Not handling negative numbers. Decide how you want to do that.
  int byteCount = (number.bitLength + 7) >> 3;

  // Special case: zero should be represented as [0], not [].
  if (byteCount == 0) {
    return Uint8List.fromList([0]);
  }

  var b256 = BigInt.from(256);
  var result = Uint8List(byteCount);
  for (int i = 0; i < byteCount; i++) {
    result[byteCount - 1 - i] = number.remainder(b256).toInt();
    number = number >> 8;
  }
  return result;
}