convertBigIntToByteList function
Convert a BigInt to a byte list. Source: https://github.com/dart-lang/sdk/issues/32803#issuecomment-1228291047
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;
}