Python: Convert Unicode-Hex-String to Unicode

后端 未结 3 1444
佛祖请我去吃肉
佛祖请我去吃肉 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:50

    This seems to work just fine:

    binascii.unhexlify(binascii.hexlify(u"Knödel".encode('utf-8'))).decode('utf-8')
    

    Comes back to the original object. You can do the same for the chinese text if it's encoded properly, however ord(x) already destroys the text you started from. You'll need to encode it first and only then treat like a string of bytes.

提交回复
热议问题