Python: Convert Unicode-Hex-String to Unicode

后端 未结 3 1443
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-05 19:35

I have a hex-string made from a unicode string with that function:

def toHex(s):
    res = \"\"
    for c in s:
        res += \"%02X\" % ord(c) #at least 2          


        
3条回答
  •  太阳男子
    2021-01-05 19:41

    Can't be done. Using %02X loses too much information. You should be using something like UTF-8 first and converting that, instead of inventing a broken encoding.

    >>> u"Knödel".encode('utf-8').encode('hex')
    '4b6ec3b664656c'
    

提交回复
热议问题