Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a byte array as hex values

Tags:

python

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

like image 277
Siavash Avatar asked Dec 27 '13 19:12

Siavash


People also ask

How do you write a byte in hexadecimal?

A byte (or octet) is 8 bits so is always represented by 2 Hex characters in the range 00 to FF.

Is byte a hexadecimal?

So a byte -- eight binary digits -- can always be represented by two hexadecimal digits. This makes hex a really great, concise way to represent a byte or group of bytes.

How do I print a byte array?

You can simply iterate the byte array and print the byte using System. out. println() method.


2 Answers

See bytes.hex():

>>> import struct
>>> struct.pack('2I',12,30).hex()   # available since Python 3.5
'0c0000001e000000'
>>> struct.pack('2I',12,30).hex(' ')  # separator available since Python 3.8
'0c 00 00 00 1e 00 00 00'
>>> struct.pack('2I',12,30).hex(' ',4) # bytes_per_sep also since Python 3.8
'0c000000 1e000000'

Older Python use binascii.hexlify:

>>> import binascii
>>> import struct
>>> binascii.hexlify(struct.pack('2I',12,30))
b'0c0000001e000000'

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'

Python 3.6+, using f-strings (but .hex() is available and easier).

>>> ' '.join(f'{n:02X}' for n in struct.pack('2I',12,33))
'0C 00 00 00 21 00 00 00'
like image 68
Mark Tolonen Avatar answered Oct 19 '22 08:10

Mark Tolonen


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']
like image 39
Lukas Graf Avatar answered Oct 19 '22 06:10

Lukas Graf