Byte Array in Python

231,527

Solution 1

In Python 3, we use the bytes object, also known as str in Python 2.

# Python 3
key = bytes([0x13, 0x00, 0x00, 0x00, 0x08, 0x00])

# Python 2
key = ''.join(chr(x) for x in [0x13, 0x00, 0x00, 0x00, 0x08, 0x00])

I find it more convenient to use the base64 module...

# Python 3
key = base64.b16decode(b'130000000800')

# Python 2
key = base64.b16decode('130000000800')

You can also use literals...

# Python 3
key = b'\x13\0\0\0\x08\0'

# Python 2
key = '\x13\0\0\0\x08\0'

Solution 2

Just use a bytearray (Python 2.6 and later) which represents a mutable sequence of bytes

>>> key = bytearray([0x13, 0x00, 0x00, 0x00, 0x08, 0x00])
>>> key
bytearray(b'\x13\x00\x00\x00\x08\x00')

Indexing get and sets the individual bytes

>>> key[0]
19
>>> key[1]=0xff
>>> key
bytearray(b'\x13\xff\x00\x00\x08\x00')

and if you need it as a str (or bytes in Python 3), it's as simple as

>>> bytes(key)
'\x13\xff\x00\x00\x08\x00'

Solution 3

An alternative that also has the added benefit of easily logging its output:

hexs = "13 00 00 00 08 00"
logging.debug(hexs)
key = bytearray.fromhex(hexs)

allows you to do easy substitutions like so:

hexs = "13 00 00 00 08 {:02X}".format(someByte)
logging.debug(hexs)
key = bytearray.fromhex(hexs)

Solution 4

Dietrich's answer is probably just the thing you need for what you describe, sending bytes, but a closer analogue to the code you've provided for example would be using the bytearray type.

>>> key = bytearray([0x13, 0x00, 0x00, 0x00, 0x08, 0x00])
>>> bytes(key)
b'\x13\x00\x00\x00\x08\x00'
>>> 
Share:
231,527

Related videos on Youtube

d0ctor
Author by

d0ctor

I'm a web developer at heart but ocassionally travel over to Python and Java when I feel adventurous. My first language was PHP (haha PHP isn't a language...) and I've been going at it ever since.

Updated on July 12, 2020

Comments

  • d0ctor
    d0ctor almost 4 years

    How can I represent a byte array (like in Java with byte[]) in Python? I'll need to send it over the wire with gevent.

    byte key[] = {0x13, 0x00, 0x00, 0x00, 0x08, 0x00};
    
  • Scott Griffiths
    Scott Griffiths almost 13 years
    Fine for Python 2.5 or earlier, but the built-in bytearray is really the way to go if you want, er, a byte array.
  • John Machin
    John Machin almost 13 years
    @TokenMacGuy: Your answer needs another 2 edits: (1) mentions the array module (2) bytearray('b', ...) doesn't work. Or you could just delete it.
  • SingleNegationElimination
    SingleNegationElimination almost 13 years
    @John: thanks, fixed. In the future, just go right ahead and make the edits yourself.
  • John Machin
    John Machin almost 13 years
    Not so simple with 3.x; fubar = str(key); print(len(key), len(fubar)) produces 6 38. In any case (1) "string" is very vague terminology (2) if he wants mutability, he can mutate his original list
  • Scott Griffiths
    Scott Griffiths almost 13 years
    @John: Good point about str working differently for bytearray in Python 3 - fixed. I mentioned mutability mainly to distinguish it from bytes, but the point is also that you don't need to have an intermediate step of having your data in a list at all.
  • John Machin
    John Machin almost 13 years
    It's a fair chance that what the OP really needs is something like struct.pack("<IH", 19, 8) ...
  • Dolda2000
    Dolda2000 over 11 years
    For the record, instead of base64.b16decode(x), you can use, simply, x.decode("hex"). It lets you get by with one less import, at least. :)
  • Mark Tolonen
    Mark Tolonen about 2 years
    bytes.fromhex('130000000800') in Python 3.