Seba
Seba

Reputation: 91

From hexadecimal string to character (jis encoding)

I have an hexadecimal string "\x98\x4F" that is the JIS encoding of the japanese kanji 楼.
How can I print the kanji in python starting from the encoding?
I tried

print b'\x98\x4F'.encode('euc_jp')

but without success... any clue? Regards

Upvotes: 2

Views: 1888

Answers (1)

mhawke
mhawke

Reputation: 87084

In Python 2 use str.decode() with the shift-jis encoding:

>>> s = "\x98\x4F".decode('shift-jis')
>>> s
u'\u697c'
>>> print s
楼

This decodes the jis encoded data into a Python unicode string. Printing that string displays the required character, provided that your default encoding can do so.

In Python 3 you can prefix the encoded string with b:

>>> s = b"\x98\x4F".decode('shift-jis')
>>> s
'楼'
>>> print(s)
楼

(this will also work in Python 2)

Upvotes: 2

Related Questions