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

时间:2022-10-12 15:47:15

I have this string: Hello world !! and I want to print it using Python as 48:65:6c:6c:6f:20:77:6f:72:6c:64:20:21:21.

我有这个字串:你好,世界!!我想用Python来打印它,就像48:65:6c: 6f:20:77:6f:72:6c:64:20:21 21。

hex() works only for integers.

hex()只对整数有效。

How can it be done?

怎么做呢?

9 个解决方案

#1


190  

Your can transform your string to a int generator, apply hex formatting for each element and intercalate with separator:

您可以将字符串转换为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

#2


148  

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

#3


50  

For Python 2.x:

对于Python 2. x:

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

The code above will not work with Python 3.x, for 3.x, the code below will work:

上面的代码不能使用Python 3。x,3。x,下面的代码将会工作:

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

#4


19  

Another answer in two lines that some might find easier to read, and helps with debugging line breaks or other odd characters in a string:

另一种回答是,有些人可能会觉得读起来更容易,并且帮助调试行中断或字符串中的其他奇怪字符:

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

#5


18  

Some complements to Fedor Gogolev answer:

Fedor Gogolev的回答:

First, if the string contains characters whose 'ASCII code' is below 10, they will not be displayed as required. In that case, the correct format should be {:02x}:

首先,如果字符串包含“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'
                                           ^^

Second, if your "string" is in reality a "byte string" -- and since the difference matters in Python 3 -- you might prefer the following:

其次,如果您的“字符串”实际上是一个“字节字符串”——而且由于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'

Please note there is no need for conversion in the above code as a bytes objects is defined as "an immutable sequence of integers in the range 0 <= x < 256".

请注意,在上面的代码中不需要转换为字节对象,它被定义为“在0 <= x < 256的范围内的一个不可变的整数序列”。

#6


7  

You can use hexdump's

您可以使用hexdump的

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

(append .lower() if you require lower-case). This works for both Python 2 & 3.

(如果你需要小写字母,就用小写字母。)这对Python 2和3都有效。

#7


7  

Print a string as hex bytes?

The accepted answer gives:

接受的答案了:

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

The accepted answer works only so long as you stick to ascii. If you use unicode, e.g.:

接受的答案只要你坚持ascii码就行。如果你使用unicode,例如:

>>> a_string = u"Привет мир!!" # "Prevyet mir, or "Hello World" in Russian.
>>> ":".join("{:02x}".format(ord(c)) for c in a_string)
'41f:440:438:432:435:442:20:43c:438:440:21:21'

We get a poor/unexpected result - these are the code points that combine to make the graphemes we see in unicode, from the unicode consortium - representing languages all over the world. This is not how we actually store this information so it can be interpreted by other sources, though.

我们得到了一个糟糕的/意想不到的结果——这些代码点结合在一起,使我们在unicode中看到的graphemes,来自unicode联盟——代表了全世界的语言。这不是我们实际存储这些信息的方式,所以它可以被其他来源解释。

To allow another source to use this data, we would usually need to convert to utf-8 encoding, for example, to save this string in bytes to disk or to publish to html. So we need that encoding to convert the code points to the code units of utf-8:

为了让另一个源使用这些数据,我们通常需要转换成utf-8编码,例如,将这个字符串保存到磁盘或发布到html中。所以我们需要编码将代码转换为utf-8的代码单元:

>>> ":".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'

Or perhaps more elegantly, just use the builtin format function:

或者更优雅地,使用builtin格式函数:

>>> ":".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'

#8


5  

Using map and lambda function can produce a list of hex values, which can be printed (or used for other purposes)

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

#9


2  

This can be done in following ways:

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

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

The output of this will be in hex as follows:

其输出将在hex中如下:

0x48 0x65 0x6c 0x6c 0x6f 0x20 0x57 0x6f 0x72 0x6c 0x64 0x20 0x21 0x21

,,,,,,,,,,,,,。

#1


190  

Your can transform your string to a int generator, apply hex formatting for each element and intercalate with separator:

您可以将字符串转换为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

#2


148  

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

#3


50  

For Python 2.x:

对于Python 2. x:

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

The code above will not work with Python 3.x, for 3.x, the code below will work:

上面的代码不能使用Python 3。x,3。x,下面的代码将会工作:

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

#4


19  

Another answer in two lines that some might find easier to read, and helps with debugging line breaks or other odd characters in a string:

另一种回答是,有些人可能会觉得读起来更容易,并且帮助调试行中断或字符串中的其他奇怪字符:

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

#5


18  

Some complements to Fedor Gogolev answer:

Fedor Gogolev的回答:

First, if the string contains characters whose 'ASCII code' is below 10, they will not be displayed as required. In that case, the correct format should be {:02x}:

首先,如果字符串包含“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'
                                           ^^

Second, if your "string" is in reality a "byte string" -- and since the difference matters in Python 3 -- you might prefer the following:

其次,如果您的“字符串”实际上是一个“字节字符串”——而且由于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'

Please note there is no need for conversion in the above code as a bytes objects is defined as "an immutable sequence of integers in the range 0 <= x < 256".

请注意,在上面的代码中不需要转换为字节对象,它被定义为“在0 <= x < 256的范围内的一个不可变的整数序列”。

#6


7  

You can use hexdump's

您可以使用hexdump的

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

(append .lower() if you require lower-case). This works for both Python 2 & 3.

(如果你需要小写字母,就用小写字母。)这对Python 2和3都有效。

#7


7  

Print a string as hex bytes?

The accepted answer gives:

接受的答案了:

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

The accepted answer works only so long as you stick to ascii. If you use unicode, e.g.:

接受的答案只要你坚持ascii码就行。如果你使用unicode,例如:

>>> a_string = u"Привет мир!!" # "Prevyet mir, or "Hello World" in Russian.
>>> ":".join("{:02x}".format(ord(c)) for c in a_string)
'41f:440:438:432:435:442:20:43c:438:440:21:21'

We get a poor/unexpected result - these are the code points that combine to make the graphemes we see in unicode, from the unicode consortium - representing languages all over the world. This is not how we actually store this information so it can be interpreted by other sources, though.

我们得到了一个糟糕的/意想不到的结果——这些代码点结合在一起,使我们在unicode中看到的graphemes,来自unicode联盟——代表了全世界的语言。这不是我们实际存储这些信息的方式,所以它可以被其他来源解释。

To allow another source to use this data, we would usually need to convert to utf-8 encoding, for example, to save this string in bytes to disk or to publish to html. So we need that encoding to convert the code points to the code units of utf-8:

为了让另一个源使用这些数据,我们通常需要转换成utf-8编码,例如,将这个字符串保存到磁盘或发布到html中。所以我们需要编码将代码转换为utf-8的代码单元:

>>> ":".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'

Or perhaps more elegantly, just use the builtin format function:

或者更优雅地,使用builtin格式函数:

>>> ":".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'

#8


5  

Using map and lambda function can produce a list of hex values, which can be printed (or used for other purposes)

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

#9


2  

This can be done in following ways:

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

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

The output of this will be in hex as follows:

其输出将在hex中如下:

0x48 0x65 0x6c 0x6c 0x6f 0x20 0x57 0x6f 0x72 0x6c 0x64 0x20 0x21 0x21

,,,,,,,,,,,,,。