def _genfmt(size, endian, sign):
    """
    Generates a format string that can be used with struct.pack()
    and struct.unpack().
    """
    if sign not in [True, False]:
        raise ValueError('sign must be either True or False')

    if endian == 'little':
        fmt = '<'
    elif endian == 'big':
        fmt = '>'
    else:
        raise ValueError('endianness must be either "little" or "big"')

    if size == 16:
        fmt += 'h' if sign else 'H'
    elif size == 32:
        fmt += 'i' if sign else 'I'
    elif size == 64:
        fmt += 'q' if sign else 'Q'
    else:
        raise ValueError('supported sizes are 16, 32 and 64')

    return fmt