[英]How do I convert hex to utf-8?
I want to convert a hex string to utf-8 我想将十六进制字符串转换为utf-8
a = '0xb3d9'
to 至
동 (http://www.unicodemap.org/details/0xB3D9/index.html)
First, obtain the integer value from the string of a
, noting that a
is expressed in hexadecimal: 首先,获得从所述串中的整数值a
,并指出a
被以十六进制表示:
a_int = int(a, 16)
Next, convert this int
to a character. 接下来,将此int
转换为字符。 In python 2 you need to use the unichr
method to do this, because the chr
method can only deal with ASCII characters: 在python 2中,您需要使用unichr
方法来执行此操作,因为chr
方法只能处理ASCII字符:
a_chr = unichr(a_int)
Whereas in python 3 you can just use the chr
method for any character: 而在python 3中,您可以仅对任何字符使用chr
方法:
a_chr = chr(a_int)
So, in python 3, the full command is: 因此,在python 3中,完整的命令是:
a_chr = chr(int(a, 16))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.