fromBytesAsSigned function

int fromBytesAsSigned(
  1. Uint8List bytes
)

Convert a list of bytes to an int value.

The value is treated as signed. The length of bytes has to be 1, 2, 4 or 8 (any other value will cause a MemException).

Example:

  Uint8List buf = [1, 1];
  int x = fromBytesAsSigned(buf); // x = 257

Implementation

int fromBytesAsSigned(Uint8List bytes) {
  int byteCnt = bytes.length;
  switch (byteCnt) {
    case 1:
      return bytes.buffer.asInt8List(0, 1)[0];
    case 2:
      return bytes.buffer.asInt16List(0, 1)[0];
    case 4:
      return bytes.buffer.asInt32List(0, 1)[0];
    case 8:
      return bytes.buffer.asInt64List(0, 1)[0];
    default:
      throw MemException("Invalid byte list length: $byteCnt");
  }
}