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

  if (bn < BigInt.zero) {
    throw 'Cannot leb encode negative values.';
  }

  List<int> pipe = [];

  while (true) {
    var i = (hexToBn(bn.toHex()) & 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);
}