[Tutor] Printing byte arrays

Ludo Joris ludo.a.joris at telenet.be
Mon Mar 7 08:02:19 EST 2022


Hello,

Fedora 35, Python 3.10.2

List of strings with 8 ones or zeros.
Convert to list of integers, all integers fit in range(256).
Convert list to bytes object (not bytearray).

Where do those spaces come from when printing this bytes object?


>>> a = ['00000000', '11100000', '11100000', '00000000', '11100000', '11100000', '00100000', '11000000', '11100000', '10100000', '10100000', '10100000', '00000000', '00100000', '00100000', '11100000', '11100000', '00100000', '00100000', '00100000', '00100000', '11100000', '11100000', '00100000', '00100000', '11000000', '11100000', '00100000', '11100000', '11000000', '00000000', '00000000', '00000000', '00000000', '00000000', '00000000', '00000000', '10000000', '11100000', '11000000', '11100000', '10000000', '00000000', '11000000', '11100000', '00100000', '11100000', '11000000', '00000000', '00100000', '11100000', '11100000', '00100000', '00000000', '00000000', '00100000', '00100000', '11100000', '11100000', '00100000', '00100000', '11000000', '11100000', '00100000']
>>> b = [int(e,2) for e in a]
>>> b
[0, 224, 224, 0, 224, 224, 32, 192, 224, 160, 160, 160, 0, 32, 32, 224, 224, 32, 32, 32, 32, 224, 224, 32, 32, 192, 224, 32, 224, 192, 0, 0, 0, 0, 0, 0, 0, 128, 224, 192, 224, 128, 0, 192, 224, 32, 224, 192, 0, 32, 224, 224, 32, 0, 0, 32, 32, 224, 224, 32, 32, 192, 224, 32]
>>> c = bytes([int(e,2) for e in a])
>>> c
b'\x00\xe0\xe0\x00\xe0\xe0 \xc0\xe0\xa0\xa0\xa0\x00  \xe0\xe0    \xe0\xe0  \xc0\xe0 \xe0\xc0\x00\x00\x00\x00\x00\x00\x00\x80\xe0\xc0\xe0\x80\x00\xc0\xe0 \xe0\xc0\x00 \xe0\xe0 \x00\x00  \xe0\xe0  \xc0\xe0 '
>>> 
>>> len(a)
64
>>> len(b)
64
>>> len(c)
64
>>> 

Thanks


More information about the Tutor mailing list