def intToBytes(i, signed=False):
    """
    Encodes an integer to bytes.

    Args:
        i (int): The integer.
        signed (bool): Whether to encode as a signed integer.

    Returns:
        bytearray: The encoded integer.
    """
    length = ((i + ((i * signed) < 0)).bit_length() + 7 + signed) // 8
    return bytearray(i.to_bytes(length, byteorder="big", signed=signed))