encodeLength static method

Uint8List encodeLength(
  1. int length, {
  2. bool longform = false,
})

Encode the given length to byte representation.

Implementation

static Uint8List encodeLength(int length, {bool longform = false}) {
  Uint8List e;
  if (length <= 127 && longform == false) {
    e = Uint8List(1);
    e[0] = length;
  } else {
    var x = Uint32List(1);
    x[0] = length;
    var y = Uint8List.view(x.buffer);
    // Skip null bytes
    var num = 3;
    while (y[num] == 0) {
      --num;
    }
    e = Uint8List(num + 2);
    e[0] = 0x80 + num + 1;
    for (var i = 1; i < e.length; ++i) {
      e[i] = y[num--];
    }
  }
  return e;
}