[英]Print a variable in hexadecimal in Python
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_hex
as 0xde 0xad 0xbe 0xef
?如何将my_hex
打印为0xde 0xad 0xbe 0xef
?
To make my question clear... Let's say I have some data like 0x01, 0x02, 0x03, 0x04
stored in a variable.为了让我的问题清楚......假设我有一些数据,如0x01, 0x02, 0x03, 0x04
存储在一个变量中。 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)
.我想我正在寻找与printf("%02x", my_hex)
等效的 Python。 I know there is print '{0:x}'.format()
, but that won't work with my_hex
and it also won't pad with zeroes.我知道有print '{0:x}'.format()
,但这不适用于my_hex
并且它也不会填充零。
You mean you have a string of bytes in my_hex
which you want to print out as hex numbers, right?你的意思是你在my_hex
有一个字节my_hex
,你想将其打印为十六进制数字,对吗? Eg, let's take your example:例如,让我们以你的例子为例:
>>> my_string = "deadbeef"
>>> my_hex = my_string.decode('hex') # python 2 only
>>> print my_hex
Þ ¾ ï
This construction only works on Python 2;此构造仅适用于 Python 2; but you could write the same string as a literal, in either Python 2 or Python 3, like this:但是您可以在 Python 2 或 Python 3 中编写与文字相同的字符串,如下所示:
my_hex = "\xde\xad\xbe\xef"
So, to the answer.所以,回答。 Here's one way to print the bytes as hex integers:这是将字节打印为十六进制整数的一种方法:
>>> 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##
.推导式将字符串拆分为字节, ord()
将每个字节转换为相应的整数, hex()
每个整数格式化为 from 0x##
。 Then we add spaces in between.然后我们在中间添加空格。
Bonus: If you use this method with unicode strings (or Python 3 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 字符串(或 Python 3 字符串)一起使用,则推导式将为您提供 unicode 字符(而不是字节),并且您将获得适当的十六进制值,即使它们大于两位数。
In Python 3 it is more likely you'll want to do this with a byte string;在 Python 3 中,您更有可能希望使用字节字符串来执行此操作; in that case, the comprehension already returns ints, so you have to leave out the ord()
part and simply call hex()
on them:在这种情况下,理解已经返回整数,因此您必须省略ord()
部分并简单地对它们调用hex()
:
>>> my_hex = b'\xde\xad\xbe\xef'
>>> print(" ".join(hex(n) for n in my_hex))
0xde 0xad 0xbe 0xef
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
>>>
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]))
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 string
as a variable name even as an example variable name is very bad practice.在一个不相关的旁注...使用string
作为变量名即使作为示例变量名也是非常糟糕的做法。
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
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'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.