2

I am trying to work with byte buffers in Python. The way I tried to go about it was to start with a zero-filled bytearray:

>>> d=bytearray(3)
>>> d
bytearray(b'\x00\x00\x00')

So far so good. I had tried to use bytes, but the documentation told me these would not be mutable. However, bytearrays should support mutable operations. Let's say I'm building some low-level fixed-sized packet, zero-padded, and I need to set the first bytes for my header. I try, because I'm lazy:

>>> d[0:1] = b'\x01\x01'
>>> d
bytearray(b'\x01\x01\x00\x00')

What gives? I started with a zero-filled bytearray precisely because I needed to keep the size constant. With the slicing I used, the answer doesn't even make sense: at worst, I expected the assignment of the two bytes to occur in both positions [0,1], adding two extra bytes to the total. Adding one extra byte just sounds nonsensical. Anyway...

Now, sure, let me try to assign each byte individually like a proper person...

>>> d[0] = b'\x01'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'bytes' object cannot be interpreted as an integer

I give up. I'm certainly not going to be working in both bytes and integers at the same time to fix this (and why was that not a problem in the previous example?). And I can't find any meaningful resource on the internet on "bytearray assignments" (the closest was a Wikiversity article which had an empty Assignments section).

Is there a sane way to manipulate bytearrays in Python? Thank you!

Python 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) [MSC v.1916 64 bit (AMD64)] on win32

7
  • d[0:1] = b'\x01\x01' will always work that way with built-in slicing, silce notation is non inclusive. Bytearrays are mutable, and can re-size. You want d[0:2] = b'\x01\x01' Note, bytearray objects require int objects for assignment (regular and slice-based). Because slice-assigning with another bytes objects will produce ints, note list(b'\x01') Commented Mar 12, 2020 at 11:39
  • Does this answer your question? How can i modify a byte array? Commented Mar 12, 2020 at 11:40
  • In other words, you need to do d[0] = 1. Commented Mar 12, 2020 at 11:41
  • @juanpa.arrivillaga Ok, I'm glad that can work then, since it will make my work easier. Can I use d[0:1] for single byte assignments then? Commented Mar 12, 2020 at 11:44
  • @RicardoM. yeah, try it. Commented Mar 12, 2020 at 11:45

1 Answer 1

3

Thanks to @juanpa.arrivillaga's comment above.

You can work with bytes in bytearray if you use non-inclusive* slice assignments, such that:

d[0:1] = b'\x01' # Single byte assignment
d[0:2] = b'\x01\x01' # Multiple byte assignments

This type of byte assignment will implicitly produce the integers needed to assign to the bytearray, instead of e.g. d[0]. If the length of the slice is smaller than the assignment, the array will grow as in my example.

(*that's just the way to slice in Python, the emphasis is just because I forgot about it in the question.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.