在 Python 中以十六进制打印变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16572008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:01:53  来源:igfitidea点击:

Print a variable in hexadecimal in Python

pythonstringhex

提问by Yaw

I'm trying to find a way to print a string in hexadecimal. For example, I have this string which I then convert to its hexadecimal value.

我正在尝试找到一种以十六进制打印字符串的方法。例如,我有这个字符串,然后我将其转换为它的十六进制值。

my_string = "deadbeef"
my_hex = my_string.decode('hex')

How can I print my_hexas 0xde 0xad 0xbe 0xef?

我怎样才能打印my_hex0xde 0xad 0xbe 0xef

To make my question clear... Let's say I have some data like 0x01, 0x02, 0x03, 0x04stored in a variable. Now I need to print it in hexadecimal so that I can read it. I guess I am looking for a Python equivalent of printf("%02x", my_hex). I know there is print '{0:x}'.format(), but that won't work with my_hexand it also won't pad with zeroes.

为了让我的问题清楚......假设我有一些数据,比如0x01, 0x02, 0x03, 0x04存储在变量中。现在我需要以十六进制打印它以便我可以阅读它。我想我正在寻找与printf("%02x", my_hex). 我知道有print '{0:x}'.format(),但这不起作用,my_hex它也不会用零填充。

采纳答案by alexis

You mean you have a string of bytesin my_hexwhich you want to print out as hex numbers, right? E.g., from your example:

你的意思是你有一个字节my_hex,你想在其中打印出十六进制数字,对吗?例如,从你的例子:

>>> my_string = "deadbeef"
>>> my_hex = my_string.decode('hex')  # python 2 only
>>> print my_hex
T - ? ?

Here's one way to do it:

这是一种方法:

>>> print " ".join(hex(ord(n)) for n in my_hex)
0xde 0xad 0xbe 0xef

The comprehension breaks the string into bytes, ord()converts each byte to the corresponding integer, and hex()formats each integer in the from 0x##. Then we add spaces in between.

推导式将字符串分解为字节,ord()将每个字节转换为相应的整数,并hex()格式化 from 中的每个整数0x##。然后我们在中间添加空格。

Bonus: If you use this method with unicode strings, the comprehension will give you unicode characters (not bytes), and you'll get the appropriate hex values even if they're larger than two digits.

奖励:如果您将此方法与 unicode 字符串一起使用,则推导式将为您提供 unicode 字符(而不是字节),即使它们大于两位数,您也将获得适当的十六进制值。

回答by Joran Beasley

Use

print " ".join("0x%s"%my_string[i:i+2] for i in range(0, len(my_string), 2))

like this:

像这样:

>>> my_string = "deadbeef"
>>> print " ".join("0x%s"%my_string[i:i+2] for i in range(0, len(my_string), 2))
0xde 0xad 0xbe 0xef
>>>

On an unrelated side note ... using stringas a variable name even as an example variable name is very bad practice.

在一个不相关的旁注......使用string作为变量名甚至作为示例变量名是非常糟糕的做法。

回答by Matt Campbell

Convert the string to an integer base 16 then to hexadecimal.

将字符串转换为以 16 为基数的整数,然后转换为十六进制。

print hex(int(string, base=16))

These are built-in functions.

这些是内置函数。

http://docs.python.org/2/library/functions.html#int

http://docs.python.org/2/library/functions.html#int

Example

例子

>>> string = 'AA'
>>> _int = int(string, base=16)
>>> _hex = hex(_int)
>>> print _int
170
>>> print _hex
0xaa
>>> 

回答by Giannis Papaioannou

You can try something like this I guess:

我猜你可以尝试这样的事情:

new_str = ""
str_value = "rojbasr"
for i in str_value:
    new_str += "0x%s " % (i.encode('hex'))
print new_str

Your output would be something like this:

您的输出将是这样的:

0x72 0x6f 0x6a 0x62 0x61 0x73 0x72

回答by Jon Clements

A way that will fail if your input string isn't valid pairs of hex characters...:

如果您的输入字符串不是有效的十六进制字符对,则会失败的一种方法......:

>>> import binascii
>>> ' '.join(hex(ord(i)) for i in binascii.unhexlify('deadbeef'))
'0xde 0xad 0xbe 0xef'

回答by Kurt Peters

Another answer with later print/format style is:

稍后打印/格式样式的另一个答案是:

res[0]=12
res[1]=23
print("my num is 0x{0:02x}{1:02x}".format(res[0],res[1]))