'Convert headers key/value to ascii/latin-1 native strings.
`charset` must be \'ascii\' or \'latin-1\'. If `mime_encode` is True and
`value` can\'t be represented in the given charset, apply MIME-encoding.'

def _convert_to_charset(self, value, charset, mime_encode=False):

    if (not isinstance(value, (bytes, str))):
        value = str(value)
    if ((isinstance(value, bytes) and (('\n' in value) or ('\r' in value))) or (isinstance(value, str) and (('\n' in value) or ('\r' in value)))):
        raise BadHeaderError(("Header    values    can't    contain    newlines    (got    %r)" % value))
    try:
        if isinstance(value, str):
            value.encode(charset)
        else:
            value = value.decode(charset)
    except UnicodeError as e:
        if mime_encode:
            value = Header(value, 'utf-8', maxlinelen=sys.maxsize).encode()
        else:
            e.reason += (',    HTTP    response    headers    must    be    in    %s    format' % charset)
            raise
    return value
