我有兴趣接受单个角色。
c = 'c' # for example
hex_val_string = char_to_hex_string(c)
print hex_val_string
输出:
63
解决这个问题的最简单方法是什么?任何预定义的字符串库的东西?
最佳答案
有几种方法可以做到这一点:
>>> hex(ord("c"))
'0x63'
>>> format(ord("c"), "x")
'63'
>>> import codecs
>>> codecs.encode(b"c", "hex")
b'63'
在 Python 2 上,您还可以使用 hex
encoding像这样(不适用于 Python 3+):
>>> "c".encode("hex")
'63'
关于python - 如何在 Python 中将单个字符转换为其十六进制 ASCII 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8088375/