以十六进制字节打印字符串

我有这个字符串: Hello, World!,我想使用 Python 将其打印为’48:65:6 c: 6c: 6f: 2c: 20:57:6 f: 72:6 c: 64:21’。

hex()仅适用于整数。

怎样才能做到呢?

476243 次浏览

可以将字符串转换为整数生成器。对每个元素应用十六进制格式并插入一个分隔符:

>>> s = "Hello, World!"
>>> ":".join("{:02x}".format(ord(c)) for c in s)
'48:65:6c:6c:6f:2c:20:57:6f:72:6c:64:21
':'.join(x.encode('hex') for x in 'Hello, World!')

对于 Python2.x:

':'.join(x.encode('hex') for x in 'Hello, World!')

以上代码将 不适用于 Python3.x。对于3.x,以下代码将工作:

':'.join(hex(ord(x))[2:] for x in 'Hello, World!')

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: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:21:21'
^^

其次,如果您的“ string”实际上是一个“ byte string”——而且在 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:21:21'

请注意,在以上代码中没有必要将 定义了一个 < em > bytes 对象转换为 “0 < = x < 256范围内的不可变整数序列”

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

对于 Python 2.7

for character in string:
print character, character.encode('hex')

对于 Python 3.7(没有在所有版本的3上进行测试)

for character in string:
print(character, character.encode('utf-8').hex())

你可以使用 hexdump:

import hexdump
hexdump.dump("Hello, World!", sep=":")

(如果需要小写字母,请附加 .lower())。

以十六进制字节打印字符串?

公认的答案是:

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'

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

a_string = u"Привет мир!!" # "Prevyet mir", or "Hello World" in Russian.

您需要以某种方式转换为字节。

如果您的终端不接受这些字符,您可以从 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}"
)

所以我们看到:

":".join("{:02x}".format(ord(c)) for c in a_string)

报税表

'41f:440:438:432:435:442:20:43c:438:440:21:21'

一个糟糕的/意想不到的结果——这些就是我们在 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'

或者更优雅地使用新的 f-string (仅在 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'

在 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'

使用 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']

这可以通过以下方式实现:

from __future__ import print_function
str = "Hello, World!"
for char in str:
mm = int(char.encode('hex'), 16)
print(hex(mm), sep=':', end=' ')

产出将以十六进制表示如下:

0x480x650x6c 0x6c 0x6f 0x200x570x6f 0x720x6c 0x640x21

只是为了方便,很简单。

def hexlify_byteString(byteString, delim="%"):
''' Very simple way to hexlify a byte string using delimiters '''
retval = ""
for intval in byteString:
retval += ('0123456789ABCDEF'[int(intval / 16)])
retval += ('0123456789ABCDEF'[int(intval % 16)])
retval += delim
return(retval[:-1])


hexlify_byteString(b'Hello, World!', ":")
# Out[439]: '48:65:6C:6C:6F:2C:20:57:6F:72:6C:64:21'

对于比 ''.format()性能更好的产品,您可以使用以下方法:

>>> ':'.join( '%02x'%(v if type(v) is int else ord(v)) for v in 'Hello, World!' )
'48:65:6C:6C:6F:2C:20:57:6F:72:6C:64:21'
>>>
>>> ':'.join( '%02x'%(v if type(v) is int else ord(v)) for v in b'Hello, World!' )
'48:65:6C:6C:6F:2C:20:57:6F:72:6C:64:21'
>>>

我很抱歉,这看起来不能更好。

如果一个人可以简单地做 '%02x'%v就好了,但是那只需要 int..。

但是,如果没有选择 ord(v)的逻辑,就只能使用字节串 b''

对于那些不关心 Python 3或冒号的人来说,这是一个更普遍的问题:

from codecs import encode


data = open('/dev/urandom', 'rb').read(20)
print(encode(data, 'hex'))      # Data


print(encode(b"hello", 'hex'))  # String

使用 f 字符串:

"".join(f"{ord(c):x}" for c in "Hello")

使用任意分隔符:

>>> "⚡".join(f"{ord(c):x}" for c in "Hello")
'48⚡65⚡6c⚡6c⚡6f'