bigIntToBytes function
Implementation
Bytes bigIntToBytes(BigInt value, int size, Endian endian,
{bool signed = false}) {
if (value < BigInt.zero && !signed) {
throw ArgumentError('Cannot convert negative bigint to unsigned.');
}
var binary = (value < BigInt.zero ? -value : value)
.toRadixString(2)
.padLeft(size * 8, '0');
if (value < BigInt.zero) {
binary = (BigInt.parse(flip(binary), radix: 2) + BigInt.one)
.toRadixString(2)
.padLeft(size * 8, '0');
}
var bytes = RegExp('[01]{8}')
.allMatches(binary)
.map((match) => int.parse(match.group(0)!, radix: 2))
.toList();
if (endian == Endian.little) {
bytes = bytes.reversed.toList();
}
return Bytes(bytes);
}