我正在寻找一种方法将字符串转换为 Python 中的转义十六进制表示形式,例如“foo”
变为 “\x66\x6f\x6f”
。
最佳答案
使用 hex
内置函数和生成器表达式:
>>> print ''.join('\\'+hex(ord(char))[1:] for char in 'foo')
\x66\x6f\x6f
关于python - 将 "foo"转换为 "\x66\x6f\x6f"的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27731094/