您的位置:首页 > 产品设计 > UI/UE

How to display a byte array as hex values

2016-07-11 18:21 603 查看


How
to display a byte array as hex values

up
vote2down
votefavorite

>>> struct.pack('2I',12, 30)
b'\x0c\x00\x00\x00\x1e\x00\x00\x00'
>>> struct.pack('2I',12, 31)
b'\x0c\x00\x00\x00\x1f\x00\x00\x00'
>>> struct.pack('2I',12, 32)
b'\x0c\x00\x00\x00 \x00\x00\x00'
^in question
>>> struct.pack('2I',12, 33)
b'\x0c\x00\x00\x00!\x00\x00\x00'
^in question


I'd like all values to display as hex

python
shareimprove
this question
edited Dec
27 '13 at 19:27

asked Dec 27 '13 at 19:23





Siavash
1,96511839

 
1 
'
' === '\x20'
'!'
=== '\x21'
 – Jan
Dvorak Dec
27 '13 at 19:25 
 
I know but how can I get it display as a hex number, so everything is uniform? – Siavash Dec
27 '13 at 19:26
 
Do you mean, in the interactive console? I'd say it makes everything to not use hex codes, as strings are not supposed
to hold unprintable characters, so you'd need to format them as hex codes manually; but then they'll show with double backslashes in the console. – Jan
Dvorak Dec
27 '13 at 19:27 
 
Im writing code to create a special packet, and for debug purposes I want to see where things are getting packed so
I'm printing it out. I could use print from my code – Siavash Dec
27 '13 at 19:29
add
a comment


3 Answers

activeoldestvotes

up vote4down
voteaccepted
How about his?
>>> data = struct.pack('2I',12, 30)
>>> [hex(ord(c)) for c in data]
['0xc', '0x0', '0x0', '0x0', '0x1e', '0x0', '0x0', '0x0']


The expression 
[item
for item in sequence]
 is a so called list
comprehension. It's basically a very compact way of writing a simple 
for
 loop,
and creating a list from the result.

The 
ord()
 builtin
function takes a string, and turns it into an integer that's its corresponding unicode code point (for characters in the ASCII character set that's the same as their value in the ASCII table).

Its counterpart, 
chr()
 for
8bit strings or 
unichr()
 for
unicode objects do the opposite.

The 
hex()
 builtin
then simply converts the integers into their hex representation.

As pointed out by @TimPeters, in Python 3 you would need to lose the 
ord()
,
because iterating over a bytes object will (already) yield integers:
Python 3.4.0a3 (default, Nov  8 2013, 18:33:56)
>>> import struct
>>> data = struct.pack('2I',12, 30)
>>> type(data)
<class 'bytes'>
>>> type(data[1])
<class 'int'>
>>>
>>> [hex(i) for i in data]
['0xc', '0x0', '0x0', '0x0', '0x1e', '0x0', '0x0', '0x0']


shareimprove
this answer
edited Dec
27 '13 at 20:01

answered Dec 27 '13 at 19:32





Lukas Graf
10.2k22446

 
3 
Note: in Python3, you have to lose the 
ord()
 -
iterating over a 
bytes
 object
gives integers. – Tim
PetersDec
27 '13 at 19:44
1 
I like this answer, but could you explain how this is working please? what do the brackets do? – SiavashDec
27 '13 at 19:44
 
@TimPeters thank you, going to update the answer accordingly. – Lukas
Graf Dec
27 '13 at 19:48
 
@Siavash sure! See my updated answer. – Lukas
Graf Dec
27 '13 at 20:01
add
a comment
up vote5down
vote
You have to reformat it yourself if you want 
\x
 escapes
everywhere; e.g.,
>>> import struct
>>> r = struct.pack('2I',12, 33)
>>> r
b'\x0c\x00\x00\x00!\x00\x00\x00'
>>> list(r)
[12, 0, 0, 0, 33, 0, 0, 0]
>>> print("".join("\\x%02x" % i for i in r))
\x0c\x00\x00\x00\x21\x00\x00\x00


shareimprove
this answer
answered Dec 27 '13 at 19:30





Tim Peters
24.2k43955

 
 
Given the desired usage, I guess joining just the hexes with spaces will do – Jan
Dvorak Dec
27 '13 at 19:34
 
@JanDvorak, if that's what the OP wants, it's trivial to change this to do that. I'm just answering the question they
asked ;-) – Tim Peters Dec
27 '13 at 19:35
add
a comment
up vote2down
vote
Try 
binascii.hexlify
:
>>> import binascii
>>> import struct
>>> binascii.hexlify(struct.pack('2I',12,30))
b'0c0000001e000000'
>>> binascii.hexlify(struct.pack('2I',12,31))
b'0c0000001f000000'
>>> binascii.hexlify(struct.pack('2I',12,32))
b'0c00000020000000'
>>> binascii.hexlify(struct.pack('2I',12,33))
b'0c00000021000000'


Or if you want spaces to make it more readable:
>>> ' '.join(format(n,'02X') for n in struct.pack('2I',12,33))
'0C 00 00 00 21 00 00 00'


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: