Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a list of bytes to a byte string

Tags:

python

hex

byte

I have the following list of bytes:

[b'S', b'\x00', b't', b'\x00', b'a', b'\x00', b'n', b'\x00', b'd', b'\x00', b'a', b'\x00', b'r', b'\x00', b'd', b'\x00', b'F', b'\x00', b'i', b'\x00', b'r', b'\x00', b'm', b'\x00', b'a', b'\x00', b't', b'\x00', b'a', b'\x00', b'.', b'\x00', b'i', b'\x00', b'n', b'\x00', b'o', b'\x00', b'y', b'\x02', b'\x03', b'S', b'\x00', b't', b'\x00', b'a', b'\x00', b'n', b'\x00', b'd', b'\x00', b'a', b'\x00', b'r', b'\x00', b'd', b'\x00', b'F', b'\x00', b'i', b'\x00', b'r', b'\x00', b'm', b'\x00', b'a', b'\x00', b't', b'\x00', b'a', b'\x00', b'.', b'\x00', b'i', b'\x00', b'n', b'\x00', b'o', b'\x00', b'y', b'\x02', b'\x03', b'S', b'\x00', b't', b'\x00', b'a', b'\x00', b'n', b'\x00', b'd', b'\x00', b'a', b'\x00', b'r', b'\x00', b'd', b'\x00', b'F', b'\x00', b'i', b'\x00', b'r', b'\x00', b'm', b'\x00', b'a', b'\x00', b't', b'\x00', b'a', b'\x00', b'.', b'\x00', b'i', b'\x00', b'n', b'\x00', b'o', b'\x00', b'y', b'\x02', b'\x03', b'S', b'\x00', b't', b'\x00', b'a', b'\x00', b'n', b'\x00', b'd', b'\x00', b'a', b'\x00', b'r',

Is there a simple way for me to convert that into one long byte? I guess techincally called a bytestring (though I am not sure about that).

Note, I looked here but it does not work for me. Also, is the correct term for one long byte object (such as b'\f\x04\55') a byte object or something else?

like image 686
Startec Avatar asked Sep 14 '14 09:09

Startec


2 Answers

Use bytes.join:

>>> b = [b'S', b'\x00', b't', b'\x00', b'a']
>>> b''.join(b)  # b'': separator
b'S\x00t\x00a'
like image 65
falsetru Avatar answered Sep 28 '22 08:09

falsetru


bytes and bytearray objects have the join method:

In [37]: b''.join([b'S', b'\x00', b't', b'\x00', b'a', b'\x00', b'n', b'\x00', b'd', b'\x00'])
Out[37]: b'S\x00t\x00a\x00n\x00d\x00'
like image 36
vaultah Avatar answered Sep 28 '22 06:09

vaultah