Reputation: 1820
I'm not sure what this type of byte array format is called in python:
Format A:
[8, 200, 1, 16, 200, 1, 24, 200, 1, 34, 21, 8, 1, 18, 17, 98, 97, 116, 116, 108, 101, 95, 116, 97]
but I'd like to convert it into this format (the name of which I’m also unsure of):
Format B:
b'\x08\xc8\x01\x10\xc8\x01\x18\xc8\x01"\x15\x08\x01\x12'
I know how to get from Format B to Format A:
import array
data =b'\x08\xc8\x01\x10\xc8'
print array.array('B', data))
but I'm unsure of how to get from Format A to Format B, and because I also don't know the names of either format, the google results I've gotten have been sparse.
Upvotes: 1
Views: 290
Reputation: 363304
Piece of cake in Python 3:
>>> L = [8, 200, 1, 16, 200, 1, 24, 200, 1, 34, 21, 8, 1, 18, 17, 98, 97, 116, 116, 108, 101, 95, 116, 97]
>>> bytes(L)
b'\x08\xc8\x01\x10\xc8\x01\x18\xc8\x01"\x15\x08\x01\x12\x11battle_ta'
If you're back on Python 2:
>>> L = [8, 200, 1, 16, 200, 1, 24, 200, 1, 34, 21, 8, 1, 18, 17, 98, 97, 116, 116, 108, 101, 95, 116, 97]
>>> str(bytearray(L))
'\x08\xc8\x01\x10\xc8\x01\x18\xc8\x01"\x15\x08\x01\x12\x11battle_ta'
Upvotes: 2