decodeSigned static method

int decodeSigned(
  1. Uint8List bytes, {
  2. int n = 64,
})

Converts a list of bytes that represent an LEB128 signed integer into an ordinary integer. The optional argument specifies the number of bits in the integer. For example, while dealing with a varint32, the second argument would be 32.

Implementation

static int decodeSigned(Uint8List bytes, {int n = 64}) {
  int result = 0;
  int shift = 0;
  int i = 0;
  while (true) {
    int byte = bytes[i];
    result |= ((byte & 0x7f) << shift);
    shift += 7;
    if ((byte & 0x80) == 0) {
      break;
    } else {
      i += 1;
    }
  }
  if ((shift < n) && (bytes[i] & 0x40) != 0) {
    result |= (~0 << shift);
  }
  return result;
}