把unicode编码的十六进制字符串转换为汉字(Python2.7)

#小端字节序转为大端字节序
def little2big_endian(hex_string):
	big_endian_str = '' #定义一个空字符串
	for i in range(len(hex_string)/4):#因为Unicode是4个字符表示一个汉字,每四个一组
		little_endian_char = hex_string[i*4: i*4+4] #取的是四位连续的数字
		big_endian_char = little_endian_char[2:4] + little_endian_char[0:2] #逆字节序
		big_endian_str = big_endian_str + big_endian_char
	return big_endian_str

#十六进制数字(大端序)转换成汉字(unicode编码)
def HextoHanzi(hex_string):
	unicode_Hanzi = '' #定义一个空字符串
	for i in range(len(hex_string)/4): #因为Unicode是4个字符表示一个汉字,每四个一组
		Hex_char = hex_string[i*4: i*4+4]
		unicode_char = "\\u" + Hex_char
		unicode_Hanzi = unicode_Hanzi + unicode_char
	return unicode_Hanzi.decode('unicode_escape')

hex_string = '1162B07328572857005F1A4F62546153A16C265EAB8EB98F'
big_endian_str = little2big_endian(hex_string)
print HextoHanzi(big_endian_str)

参考资料:[1] Python——将用unicode编码的十六进制数字转换为汉字   [2] python3 汉字转十六进制unicode

你可能感兴趣的:(Python学习)