[英]How to print hex dump from a buffer in python?
I have a python snippet like this: 我有一个像这样的python代码段:
siz=len(ret)
for i in range(0,siz-1):
print "%s " % ret[i],
When I use %s
it works fine and prints some alien characters on the console!! 当我使用%s
它可以正常工作并在控制台上打印一些外来字符!
But how do I print hex dump of it. 但是我如何打印它的十六进制转储。
I tried: 我试过了:
print "%02x " % ret[i],
print "%02x " % hex(ret[i]),
print format(ret[i],'02x'),
print format(hex(ret[i],'02x'),
print "%02x " % hex(int(ret[i])),
All these things resulted in errors. 所有这些都导致错误。
A similar question is asked here but those answers didn't help me. 在这里提出了类似的问题,但是这些答案对我没有帮助。
How do I do this similar to c
style printf("%02x ",ret[i]);
我该如何类似于c
样式printf("%02x ",ret[i]);
I think you are looking for the character code which you can obtain with ord
, then you can apply your method to print the character code hexadecimal: 我认为您正在寻找可以使用ord
获得的字符代码,然后可以应用您的方法来打印十六进制的字符代码:
ret="#~½|"
for c in ret:
print "%s - %02x" % (c,ord(c))
Which gives as output: 输出为:
# - 23
~ - 7e
� - c2
� - bd
| - 7c
4 - 34
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.