def bytes_to_int(b: bytes):
    """
    Convert a byte array to an integer.

    :param b: the byte array
    :return: the integer
    """
    neg = False  # Have we determined the value to be negative?  We'll see.
    i=0  # This is the variable in which we'll store the value.
    # Now we need to do some tap dancing to handle negative integers...
    # We can't directly modify the original bytearray, so let's create a new variable to reference it.
    _bytes = b
    # If we discover that the highest-order bit in the higest-order byte (which should be at index 0) is flipped on...
    if _bytes[0] >= 128:
        # ...we need to create a new list of bite-sized integers.
        _bytes = list(b)
        # Now we flip the higest-order bit off so processing below can continue without worrying about negatives...
        _bytes[0] = _bytes[0] & 127
        # ...but we'll make note that the final result *is* negative.
        neg = True
    # Now we need to know how many bytes (or byte-sized ints) we're dealing with.
    width = len(_bytes)
    # Let's go through them all, starting with the highest-order byte.
    for idx in range(0, width):
        # Since the higest-order bytes are in the lower indexes, we'll bit shift each value *fewer* positions to the
        # left as we move from lower index values to higher index values.
        i_at_idx = _bytes[idx] << (width - idx - 1) * 8  # (Shift it 8 bits, or 1 byte.)
        # Now we can just add it!
        i = i + i_at_idx
    # Return the number we have (or its negative twin).
    return i if not neg else i * -1