lebEncode function

Uint8List lebEncode(
  1. dynamic value
)

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 = value is BigInt ? value : BigInt.from(value);
  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);
}