lebEncode function
Encode a positive number (or bigint) into a Buffer. The number will be floored to the nearest integer. @param value The number to encode.
Implementation
Uint8List lebEncode(dynamic value) {
BigInt bn = switch (value) {
BigInt() => value,
num() => BigInt.from(value),
String() => BigInt.parse(value),
_ => throw ArgumentError('Invalid big number: $value', 'lebEncode'),
};
if (bn < BigInt.zero) {
throw StateError('Cannot leb-encode negative values.');
}
final List<int> pipe = [];
while (true) {
final i = (hexToBn(bn.toHex(include0x: true)) & BigInt.from(0x7f)).toInt();
bn = bn ~/ BigInt.from(0x80);
if (bn == BigInt.zero) {
pipe.add(i);
break;
} else {
pipe.add(i | 0x80);
}
}
return Uint8List.fromList(pipe);
}