纯十六进制形式的 Python 字符串

标签 python

>>> x = "\x55\x89\xe5\xb9\x00\x00\x00\x00\xb8\xc0\xa0\x04\x08\x31\xdb\x31\xd2\x8a\x1c\x08\x01\xda\x41\x83\xf9\x72\x75\xf5\x81\xfa\x71\x71\x00\x00\x74\x22\xb8\x04\x00\x00\x00\xbb\x01\x00\x00\x00\xb9\x7a\xa0\x04\x08\xba\x0a\x00\x00\x00\xcd\x80\xb8\x01\x00\x00\x00\xbb\x00\x00\x00\x00\xcd\x80\xb8\x04\x00\x00\x00\xbb\x01\x00\x00\x00\xb9\x70\xa0\x04\x08\xba\x09\x00\x00\x00\xcd\x80\xb8\x01\x00\x00\x00\xbb\x00\x00\x00\x00\xcd\x80\x5d\xc3"
>>> x
'U\x89\xe5\xb9\x00\x00\x00\x00\xb8\xc0\xa0\x04\x081\xdb1\xd2\x8a\x1c\x08\x01\xdaA\x83\xf9ru\xf5\x81\xfaqq\x00\x00t"\xb8\x04\x00\x00\x00\xbb\x01\x00\x00\x00\xb9z\xa0\x04\x08\xba\n\x00\x00\x00\xcd\x80\xb8\x01\x00\x00\x00\xbb\x00\x00\x00\x00\xcd\x80\xb8\x04\x00\x00\x00\xbb\x01\x00\x00\x00\xb9p\xa0\x04\x08\xba\t\x00\x00\x00\xcd\x80\xb8\x01\x00\x00\x00\xbb\x00\x00\x00\x00\xcd\x80]\xc3'

有什么方法可以让解释器完全按照我输入的内容打印出 x 吗?即它应该以 \x55 开头,而不是 U

最佳答案

是的,在引号前添加r:

x = r"\x55\x89\xe5\xb9\x00\x00\x00\x00\xb8\xc0\xa0\x04\x08\x31\xdb\x31\xd2\x8a\x1c\x08\x01\xda\x41\x83\xf9\x72\x75\xf5\x81\xfa\x71\x71\x00\x00\x74\x22\xb8\x04\x00\x00\x00\xbb\x01\x00\x00\x00\xb9\x7a\xa0\x04\x08\xba\x0a\x00\x00\x00\xcd\x80\xb8\x01\x00\x00\x00\xbb\x00\x00\x00\x00\xcd\x80\xb8\x04\x00\x00\x00\xbb\x01\x00\x00\x00\xb9\x70\xa0\x04\x08\xba\x09\x00\x00\x00\xcd\x80\xb8\x01\x00\x00\x00\xbb\x00\x00\x00\x00\xcd\x80\x5d\xc3"
print x

输出

\x55\x89\xe5\xb9\x00\x00\x00\x00\xb8\xc0\xa0\x04\x08\x31\xdb\x31\xd2\x8a\x1c\x08\x01\xda\x41\x83\xf9\x72\x75\xf5\x81\xfa\x71\x71\x00\x00\x74\x22\xb8\x04\x00\x00\x00\xbb\x01\x00\x00\x00\xb9\x7a\xa0\x04\x08\xba\x0a\x00\x00\x00\xcd\x80\xb8\x01\x00\x00\x00\xbb\x00\x00\x00\x00\xcd\x80\xb8\x04\x00\x00\x00\xbb\x01\x00\x00\x00\xb9\x70\xa0\x04\x08\xba\x09\x00\x00\x00\xcd\x80\xb8\x01\x00\x00\x00\xbb\x00\x00\x00\x00\xcd\x80\x5d\xc3

关于纯十六进制形式的 Python 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25008743/

相关文章:

python - Python 2.7 中的断言对我不起作用示例 assertIn

python - 如何从 MZ 可执行文件中提取 Unicode 字符序列?

python - 理解tensorflow中的 `tf.nn.nce_loss()`

python - 在 Python 中关闭 CSV 文件

python - 无法将 dict 保存到 mat 文件

python - 谷歌应用引擎 : No module named google. API

python - 根据某些列条件从 Pandas 数据框中获取所有行组合?

python - 无法在 matplotlib x 轴上显示 Pandas 日期索引

python - Pyside QPushButton与matplotlib的连接

python - Django 对 URL 的覆盖率测试为 0%,为什么?