如何将 Python 中的字符串转换为其 ASCII 十六进制表示形式?
示例: 我想要结果 '\x00\x1b\xd4}\xa4\xf3\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
在 001bd47da4f3
中。
最佳答案
>>> text = '\x00\x1b\xd4}\xa4\xf3\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'.rstrip('\0')
>>> print "".join("%02x" % ord(c) for c in text)
001bd47da4f3
根据 martineau 的评论,这是 Python 3 的方式:
>>> "".join(format(ord(c),"02x") for c in text)
关于python - 将 Python 字符串转换为其 ASCII 表示形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3850531/