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.

Implementation

static int fromBytes(
  List<int> bytes, {
  Endian byteOrder = Endian.big,
  bool sign = false,
}) {
  if (bytes.length > 6) {
    final big = BigintUtils.fromBytes(
      bytes,
      byteOrder: byteOrder,
      sign: sign,
    );
    if (big.isValidInt) {
      return big.toInt();
    }
    throw ArgumentException.invalidOperationArguments(
      "fromBytes",
      name: "byteint",
      reason: "Value too large to fit in a Dart int.",
    );
  }
  if (byteOrder == Endian.little) {
    bytes = 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;
}