intToBytes function
Implementation
Bytes intToBytes(int value, int size, Endian endian, {bool signed = false}) {
if (value < 0 && !signed) {
throw ArgumentError('Cannot convert negative int to unsigned.');
}
var binary =
(value < 0 ? -value : value).toRadixString(2).padLeft(size * 8, '0');
if (value < 0) {
binary = (int.parse(flip(binary), radix: 2) + 1)
.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);
}