python 3 Unicode文字と16進符号化の相互回転(単一文字)
3955 ワード
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(' '))
転載先:https://www.cnblogs.com/yanghao2008/p/10755616.html