Christian
Christian

Reputation: 26427

Is there something like ord() in python that gives the unicode hex?

I want to transfer unicode into asci characters, transfer them through a channel that only accepts asci characters and then transform them back into proper unicode.

I'm dealing with the unicode characters like ɑ in Python 3.5.

ord("ɑ") gives me 63 with is the same as what ord("?") also gives me 63. This means simply using ord() and chr() doesn't work. How do I get the right conversion?

Upvotes: 0

Views: 667

Answers (3)

jfs
jfs

Reputation: 414745

I want to transfer unicode into ascii characters, transfer them through a channel that only accepts ascii characters and then transform them back into proper unicode.

>>> import json
>>> json.dumps('ɑ')
'"\\u0251"'
>>> json.loads(_)
'ɑ'

Upvotes: 1

Christian
Christian

Reputation: 26427

I found my error. I used Python via the Windows console and the Windows console mishandeled the unicode.

Upvotes: 0

Paul Virally
Paul Virally

Reputation: 156

You can convert a number to a hex string with "0x%x" %255 where 255 would be the number you want to convert to hex.

To do this with ord, you could do "0x%x" %ord("a") or whatever character you want.

You can remove the 0x part of the string if you don't need it. If you want to hex to be capitalized (A-F) use "0x%X" %ord("a")

Upvotes: 1

Related Questions