0

I want to convert a hex string to utf-8

 a = '0xb3d9'

to

 동 (http://www.unicodemap.org/details/0xB3D9/index.html)
Cœur
  • 37,241
  • 25
  • 195
  • 267
M2KS
  • 47
  • 2
  • 6

1 Answers1

2

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))
Rob Bricheno
  • 4,467
  • 15
  • 29
  • The problem is 'ValueError: chr() arg not in range(256)'. This character is multibyte. I do not know how to convert multibyte to character. – M2KS Oct 12 '18 at 10:28
  • That means you are using python 2. So use `a_chr = unichr(int(a, 16))` – Rob Bricheno Oct 12 '18 at 10:30
  • 1
    Oh ... It was my mistake. I was stupid for two hours. haha stupid mistake – M2KS Oct 12 '18 at 10:36