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)
Aes*_*ete 157
':'.join(x.encode('hex') for x in 'Hello World!')
Run Code Online (Sandbox Code Playgroud)
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)
cop*_*300 20
另外两行中的答案有些人可能会更容易阅读,并有助于调试字符串中的换行符或其他奇怪字符:
for character in string:
print character, character.encode('hex')
Run Code Online (Sandbox Code Playgroud)
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中,传递c给ord第一个,即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)
您可以使用 hexdump的
import hexdump
hexdump.dump("Hello World", sep=":")
Run Code Online (Sandbox Code Playgroud)
(.lower()如果您需要小写,请附加).这适用于Python 2和3.
使用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)
对于那些不关心 Python\xc2\xa03 或冒号的人来说更通用一些:
\nfrom codecs import encode\n\ndata = open('/dev/urandom', 'rb').read(20)\nprint(encode(data, 'hex')) # Data\n\nprint(encode(b"hello", 'hex')) # String\nRun Code Online (Sandbox Code Playgroud)\n