1

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.

Eden
  • 1,782
  • 15
  • 23
  • I've found the answer you're looking for here: https://stackoverflow.com/questions/3470398/list-of-integers-into-string-byte-array-python array.array('B', data).tostring() – Jace J McPherson Jul 19 '17 at 23:31

1 Answers1

2

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'
wim
  • 338,267
  • 99
  • 616
  • 750