python3 Unicode字符与16进制编码互转(单个字符)
def pad(text): while len(text) % 4 != 0: text = '0'+text return text def Unicode2HexChar(Unicde_Char):#Unicode转16进制 Hex_Char = "" for i in range(0, len(Unicde_Char)): Hex_Char += (hex(ord(Unicde_Char[i])).replace('0x','').zfill(4)) return Hex_Char print(Unicode2HexChar('。')) def HexChar2Unicode(Hex_Char):#16进制转Unicode Unicode_Char='' if len(Hex_Char)>8:#Unicode编码不能超过8位 return None if len(Hex_Char)==4: Unicode_Char='\\u'+Hex_Char if len(Hex_Char)<4: Unicode_Char='\\u'+pad(Hex_Char) if len(Hex_Char)>4: Unicode_Char='\\U'+pad(Hex_Char) return Unicode_Char.encode().decode('unicode_escape') print(HexChar2Unicode('ADA')) def chineseToUnic(ch):#中文字符转16进制 return ch.encode('unicode_escape')[2:].decode('utf-8') print(chineseToUnic('飓'))