[英]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. 我想将unicode转换为asci字符,通过仅接受asci字符的通道传输它们,然后将其转换回正确的unicode。
I'm dealing with the unicode characters like ɑ
in Python 3.5. 我处理中的Unicode字符像
ɑ
在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. ord("ɑ")
给我63与ord("?")
也给我63相同。这意味着仅使用ord()
和chr()
不起作用。 How do I get the right conversion? 如何获得正确的转化?
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. 您可以将数字转换为带有
"0x%x" %255
的十六进制字符串,其中255是要转换为十六进制的数字。
To do this with ord, you could do "0x%x" %ord("a")
or whatever character you want. 为此,您可以使用
"0x%x" %ord("a")
或任何所需的字符。
You can remove the 0x part of the string if you don't need it. 如果不需要,可以删除字符串的0x部分。 If you want to hex to be capitalized (AF) use
"0x%X" %ord("a")
如果要十六进制大写(AF),请使用
"0x%X" %ord("a")
I found my error. 我发现了我的错误。 I used Python via the Windows console and the Windows console mishandeled the unicode.
我通过Windows控制台使用Python,而Windows控制台对Unicode的处理不当。
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.
我想将unicode转换为ASCII字符,通过仅接受ASCII字符的通道传输它们,然后将其转换回正确的unicode。
>>> import json
>>> json.dumps('ɑ')
'"\\u0251"'
>>> json.loads(_)
'ɑ'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.