将字符串打印为十六进制字节?

Edu*_*scu 152 python string hex ordinal-indicator

我有这个字符串:Hello world !!我想用Python打印它48:65:6c:6c:6f:20:77:6f:72:6c:64:20:21:21.

hex() 仅适用于整数.

怎么做到呢?

Fed*_*lev 218

您可以将字符串转换为int生成器,为每个元素应用十六进制格式并插入分隔符:

>>> s = "Hello world !!"
>>> ":".join("{:02x}".format(ord(c)) for c in s)
'48:65:6c:6c:6f:20:77:6f:72:6c:64:20:21:21
Run Code Online (Sandbox Code Playgroud)

  • 实际上,这会在python3中产生无效输出:`":".join("{{02x}".format(ord(c))for cin'løl')`返回`'6c:f8:6c'`,而'":".加入("{{02x}".格式(c)为'løl'.encode()中的c'生成正确的utf-8表示`'6c:c3:b8:6c'. (7认同)
  • 请注意,在python3中,将`str`打印为十六进制的概念并不真正有意义; 你想要将`bytes`对象打印为hex(通过调用`.encode()`将`str`转换为`bytes`). (2认同)
  • 这个问题和答案有点假设您的输入从不包含非 ASCII 字符。如果您的输入可能包含表情符号或非基于拉丁语的书写系统等内容,您可能需要使用 `":".join("{:04x}".format(ord(c)) for c in s)` (替换`02x` 和 `04x` 将每个数字用零填充为 4 位数字) (2认同)

Aes*_*ete 157

':'.join(x.encode('hex') for x in 'Hello World!')
Run Code Online (Sandbox Code Playgroud)

  • @hyh:`h = binascii.hexlify(b"Hello world !!")获取十六进制字符串.b":".join(h [i:i + 2] for i in range(0,len(h),2))`在每两个十六进制数字后插入`':'`. (6认同)
  • 不适用于 Python 3。`LookupError: 'hex' 不是文本编码;使用 codecs.encode() 处理任意编解码器` (4认同)
  • 如何在python3中执行此操作? (3认同)

Kel*_* Hu 54

对于Python 2.x:

':'.join(x.encode('hex') for x in 'Hello World!')
Run Code Online (Sandbox Code Playgroud)

上面的代码不适用于Python 3.x,对于3.x,下面的代码将起作用:

':'.join(hex(ord(x))[2:] for x in 'Hello World!')
Run Code Online (Sandbox Code Playgroud)

  • 在其他用户提供这两种解决方案的几个月后,您为什么决定将此作为新答案发布?如果重点是澄清版本兼容性,那么建议对现有答案进行编辑会更有意义. (3认同)
  • 但还要注意,后者不填充前导零: hex(ord("\x00"))[2:] 是 "0" 和 "\x00".encode("hex") == "00" (2认同)
  • 正如其他地方所指出的,一旦超出 ascii 并考虑 unicode,这个答案甚至都不正确。':'.join(hex(ord(x))[2:] for x in 'løl') 错误地打印了 '6c:f8:6c' 而正确的输出是 '6c:c3:b8:6c'。 (2认同)

cop*_*300 20

另外两行中的答案有些人可能会更容易阅读,并有助于调试字符串中的换行符或其他奇怪字符:

for character in string:
  print character, character.encode('hex')
Run Code Online (Sandbox Code Playgroud)

  • 啊,非常感谢您提供的信息...是的,这绝对是为 Python 2.7 编写的。我将更新我的答案以包括如何针对 Python 3.7 执行此操作。 (2认同)

Syl*_*oux 19

Fedor Gogolev的一些补充回答:

首先,如果字符串包含"ASCII代码"低于10的字符,则不会按要求显示它们.在这种情况下,正确的格式应该是{:02x}:

>>> s = "Hello unicode \u0005 !!"
>>> ":".join("{0:x}".format(ord(c)) for c in s)
'48:65:6c:6c:6f:20:75:6e:69:63:6f:64:65:20:5:20:21:21'
                                           ^

>>> ":".join("{:02x}".format(ord(c)) for c in s)
'48:65:6c:6c:6f:20:75:6e:69:63:6f:64:65:20:05:20:21:21'
                                           ^^
Run Code Online (Sandbox Code Playgroud)

其次,如果你的"字符串"实际上是一个"字节字符串" - 并且由于差异在Python 3中很重要 - 你可能更喜欢以下内容:

>>> s = b"Hello bytes \x05 !!"
>>> ":".join("{:02x}".format(c) for c in s)
'48:65:6c:6c:6f:20:62:79:74:65:73:20:05:20:21:21'
Run Code Online (Sandbox Code Playgroud)

请注意,上面的代码不需要转换,因为字节对象被定义"0 <= x <256"范围内的整数的不可变序列.


Aar*_*all 10

将字符串打印为十六进制字节?

接受的答案给出:

s = "Hello world !!"
":".join("{:02x}".format(ord(c)) for c in s)
Run Code Online (Sandbox Code Playgroud)

收益:

'48:65:6c:6c:6f:20:77:6f:72:6c:64:20:21:21'
Run Code Online (Sandbox Code Playgroud)

只有在使用字节(主要是ascii字符)时,接受的答案才有效.但是如果你使用unicode,例如:

a_string = u"?????? ???!!" # "Prevyet mir", or "Hello World" in Russian.
Run Code Online (Sandbox Code Playgroud)

你需要以某种方式转换为字节.

如果您的终端不接受这些字符,您可以从utf-8解码或使用名称(这样您就可以粘贴并运行代码):

a_string = (
    "\N{CYRILLIC CAPITAL LETTER PE}"
    "\N{CYRILLIC SMALL LETTER ER}"
    "\N{CYRILLIC SMALL LETTER I}"
    "\N{CYRILLIC SMALL LETTER VE}"
    "\N{CYRILLIC SMALL LETTER IE}"
    "\N{CYRILLIC SMALL LETTER TE}"
    "\N{SPACE}"
    "\N{CYRILLIC SMALL LETTER EM}"
    "\N{CYRILLIC SMALL LETTER I}"
    "\N{CYRILLIC SMALL LETTER ER}"
    "\N{EXCLAMATION MARK}"
    "\N{EXCLAMATION MARK}"
)
Run Code Online (Sandbox Code Playgroud)

所以我们看到:

":".join("{:02x}".format(ord(c)) for c in a_string)
Run Code Online (Sandbox Code Playgroud)

回报

'41f:440:438:432:435:442:20:43c:438:440:21:21'
Run Code Online (Sandbox Code Playgroud)

一个糟糕/意想不到的结果 - 这些代码点结合在一起形成我们在unicode中看到的字形,来自unicode联盟 - 代表全世界的语言.这不是我们实际存储此信息的方式,因此可以由其他来源解释.

要允许其他源使用此数据,我们通常需要转换为utf-8编码,例如,将此字符串以字节为单位保存到磁盘或发布到html.所以我们需要编码将代码点转换为utf-8 的代码单元 - 在Python 3中,ord不需要因为bytes是整数的迭代:

>>> ":".join("{:02x}".format(c) for c in a_string.encode('utf-8'))
'd0:9f:d1:80:d0:b8:d0:b2:d0:b5:d1:82:20:d0:bc:d0:b8:d1:80:21:21'
Run Code Online (Sandbox Code Playgroud)

或者更优雅的是,使用新的f字符串(仅在Python 3中可用):

>>> ":".join(f'{c:02x}' for c in a_string.encode('utf-8'))
'd0:9f:d1:80:d0:b8:d0:b2:d0:b5:d1:82:20:d0:bc:d0:b8:d1:80:21:21'
Run Code Online (Sandbox Code Playgroud)

在Python 2中,传递cord第一个,即ord(c)- 更多示例:

>>> ":".join("{:02x}".format(ord(c)) for c in a_string.encode('utf-8'))
'd0:9f:d1:80:d0:b8:d0:b2:d0:b5:d1:82:20:d0:bc:d0:b8:d1:80:21:21'
>>> ":".join(format(ord(c), '02x') for c in a_string.encode('utf-8'))
'd0:9f:d1:80:d0:b8:d0:b2:d0:b5:d1:82:20:d0:bc:d0:b8:d1:80:21:21'
Run Code Online (Sandbox Code Playgroud)

  • @not2qubit 请再次尝试这些示例 - 我花了一点时间来解决 Python 2 和 3 之间的差异,显然我最初只是为 Python 2 编写这些示例。感谢对我的回答进行 QA! (2认同)

Tob*_*ler 7

您可以使用 hexdump

import hexdump
hexdump.dump("Hello World", sep=":")
Run Code Online (Sandbox Code Playgroud)

(.lower()如果您需要小写,请附加).这适用于Python 2和3.


Bre*_*mon 6

使用map和lambda函数可以生成十六进制值列表,可以打印(或用于其他目的)

>>> s = 'Hello 1 2 3 \x01\x02\x03 :)'

>>> map(lambda c: hex(ord(c)), s)
['0x48', '0x65', '0x6c', '0x6c', '0x6f', '0x20', '0x31', '0x20', '0x32', '0x20', '0x33', '0x20', '0x1', '0x2', '0x3', '0x20', '0x3a', '0x29']
Run Code Online (Sandbox Code Playgroud)


Gri*_*ave 5

对于那些不关心 Python\xc2\xa03 或冒号的人来说更通用一些:

\n
from codecs import encode\n\ndata = open('/dev/urandom', 'rb').read(20)\nprint(encode(data, 'hex'))      # Data\n\nprint(encode(b"hello", 'hex'))  # String\n
Run Code Online (Sandbox Code Playgroud)\n