Python: Convert Unicode-Hex-String to Unicode -
i have hex-string made unicode string function:
def tohex(s): res = "" c in s: res += "%02x" % ord(c) #at least 2 hex digits, can more return res hex_str = tohex(u"...")
this returns string one:
"80547cfb4eba5df15b585728"
that's sequence of 6 chinese symbols.
u"knödel"
converts to
"4b6ef664656c"
what need function convert original unicode. chinese symbols seem have 2-byte representation while second example has 1-byte representations characters. can't use unichr() each 1- or 2-byte block.
i've tried
binascii.unhexlify(hex_str)
but seems convert byte-by-byte , returns string, not unicode. i've tried
binascii.unhexlify(hex_str).decode(...)
with different formats. never got original unicode string.
thank lot in advance!
this seems work fine:
binascii.unhexlify(binascii.hexlify(u"knödel".encode('utf-8'))).decode('utf-8')
comes original object. can same chinese text if it's encoded properly, ord(x)
destroys text started from. you'll need encode first , treat string of bytes.
Comments
Post a Comment