fromBytes static method

int fromBytes(
  1. List<int> bytes, {
  2. Endian byteOrder = Endian.big,
  3. bool sign = false,
})

Converts a list of bytes to an integer, following the specified byte order.

bytes The list of bytes representing the integer value. byteOrder The byte order, defaults to Endian.big. Returns the corresponding integer value.

Implementation

static int fromBytes(List<int> bytes,
    {Endian byteOrder = Endian.big, bool sign = false}) {
  assert(bytes.length <= 8);
  if (byteOrder == Endian.little) {
    bytes = List<int>.from(bytes.reversed.toList());
  }
  int result = 0;
  if (bytes.length > 4) {
    final int lowerPart =
        fromBytes(bytes.sublist(bytes.length - 4, bytes.length));
    final int upperPart = fromBytes(bytes.sublist(0, bytes.length - 4));
    result = (upperPart << 32) | lowerPart;
  } else {
    for (var i = 0; i < bytes.length; i++) {
      result |= (bytes[bytes.length - i - 1] << (8 * i));
    }
  }

  if (sign && (bytes[0] & 0x80) != 0) {
    return result.toSigned(bitlengthInBytes(result) * 8);
  }

  return result;
}