Python 根据 汉字的 unicode 编码(int类型)得到汉字字符?

39532 是汉字 "马" 的 unicode 编码(十进制整形),怎么转换成汉字?
2025-01-03 23:00:15
推荐回答(1个)
回答1:

def decimal2hex(number):
return hex(number)[2:]

def hex2unicode(hex_number_string):
return '\\u' + hex_number_string

def unicode2chinese(unicode_number):
if isinstance(unicode_number, str):
return unicode_number.encode("latin-1").decode("unicode-escape")
elif isinstance(unicode_number, bytes):
return unicode_number.decode("unicode-escape")

if __name__ == "__main__":
number = 39532
hex_number_string = decimal2hex(number)
unicode_number = hex2unicode(hex_number_string)
chinese = unicode2chinese(unicode_number)
print(chinese)