M2KS
M2KS

Reputation: 47

How do I convert hex to utf-8?

I want to convert a hex string to utf-8

 a = '0xb3d9'

to

 동 (http://www.unicodemap.org/details/0xB3D9/index.html)

Upvotes: 0

Views: 4206

Answers (1)

Rob Bricheno
Rob Bricheno

Reputation: 4653

First, obtain the integer value from the string of a, noting that a is expressed in hexadecimal:

a_int = int(a, 16)

Next, convert this int to a character. In python 2 you need to use the unichr method to do this, because the chr method can only deal with ASCII characters:

a_chr = unichr(a_int)

Whereas in python 3 you can just use the chr method for any character:

a_chr = chr(a_int)

So, in python 3, the full command is:

a_chr = chr(int(a, 16))

Upvotes: 2

Related Questions