Converts an int to a byte array (bytes).
    :param val: value to encode
    :param bit: bit length of the integer to encode
    :param signed: encode as unsigned int if false
    :param big_endian: encode with big or little endian
    :return:

def int_to_bytes(val, bit=32, signed=False, big_endian=True):
    """
    Converts an int to a byte array (bytes).
    :param val: value to encode
    :param bit: bit length of the integer to encode
    :param signed: encode as unsigned int if false
    :param big_endian: encode with big or little endian
    :return:
    """
    val = int(val) #ensure it is an int

    if six.PY3:
        order = 'little'
        if big_endian:
            order = 'big'
        return val.to_bytes(length=bit//8, byteorder=order, signed=signed)

    if bit == 8:
        code = 'B'
    elif bit == 16:
        code = 'H'
    elif bit == 32:
        code = 'I'
    elif bit == 64:
        code = 'Q'
    else:
        raise Exception("int_to_bytes : size parameter value should be 8, 16, 32, or 64")

    if big_endian:
        code = '>'+code
    else:
        code = '<'+code

    if signed or val < 0:
        code = code.lower()

    return struct.pack(code, val)