简体   繁体   English

在 Python 中,如何将十六进制数转换为其对应的 unicode 字符?

[英]In Python, how to convert a hexadecimal number to its corresponding unicode character?

I want to convert a hexadecimal number such as '0x0391' to its corresponding Unicode character (in this example is the Greek letter A).我想将一个十六进制数字(例如“0x0391”)转换为其相应的 Unicode 字符(在本例中是希腊字母 A)。 I have tried to concat the four hexadecimal digits (stored in a variable h) to the end of "\\u\u0026quot;, like "\\u\u0026quot;+h or f"\\u{h}\u0026quot;.我试图将四个十六进制数字(存储在变量 h 中)连接到“\\u”的末尾,例如“\\u”+h 或 f“\\u{h}”。 Both attempts produced an error.两次尝试都产生了错误。 It seems that python will check the escape character before substitute the values in the f-string.似乎python会在替换f字符串中的值之前检查转义字符。 How can I resolve this problem?我该如何解决这个问题?

If you have the number available as a string, parse it and pass it to chr :如果您有可用字符串形式的数字,请解析它并将其传递给chr

> chr(int('0x0391', base=16))
'Α'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM