Python bytes concatenation

If you want to change your byte sequence, you should use a bytearray. It is mutable and has the .append method:

>>> a = bytearray(b'\x14\xf6')
>>> a.append(a[0])
>>> a
bytearray(b'\x14\xf6\x14')

What happens in your approach: when you do

a += a[0]

you are trying to add an integer to a bytes object. That doesn’t make sense, since you are trying to add different types.

If you do

bytes(a[0])

you get a bytes object of length 20, as the documentation describes:

If [the argument] is an integer, the array will have that size and will be initialized with null bytes.

If you use curly braces, you are creating a set, and a different option in the constructor is chosen:

If it is an iterable, it must be an iterable of integers in the range 0 <= x < 256, which are used as the initial contents of the array.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)