encodeLength static method

Uint8List encodeLength(
  1. int length
)

Encode a BER length - into 1 to 5 bytes as appropriate Values less than <= 127 are encoded in a single byte If the value is larger than 127, the first byte contains 0x80 + the number of following bytes used to represent the length The length is encoded by the ///fewest/// number of bytes possible - treated as an unsigned binary value.

This is a static method that has no side effect on an object. The returned bytes can be copied into an encoded representation of an object.

Implementation

static Uint8List encodeLength(int length) {
  Uint8List e;
  if (length <= 127) {
    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;
}