Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
appending to a bytearray
#11
As mentioned, you append() bytes. Since each element from the bytearray is a byte, that should work fine.

>>> b = bytearray()
>>> for x in bytearray(b'abc'):
...   b.append(x)
...
>>> b
bytearray(b'abc')
Reply
#12
You don't do this:
Quote:>>> b=bytearray()
>>> for x in bytearray(b'abc'):
... b.extend(x)
You do this:
>>> b.extend(b'abc')
If you want to append x, and you don't know what x is, you could do some duck typing.
junk = (5, (1, 2, 3), b"123")
my_byte_array = bytearray(b"This is my bytearray: ")
for thing in junk:
    try:
        my_byte_array.extend(thing)
    except TypeError:
        my_byte_array.append(thing)

print(my_byte_array)
Or if you wanted to be a little more specific:
junk = (5, (1, 2, 3), b"123")
my_byte_array = bytearray(b"This is my bytearray: ")
for thing in junk:
    try:
        iter(thing)
    except TypeError:
        my_byte_array.append(thing)
    else:
        my_byte_array.extend(thing)

print(my_byte_array)
Output:
bytearray(b'This is my bytearray: \x05\x01\x02\x03123')
Reply
#13
each element from a bytes is a byte, also. it wants ints. so a bytearray or bytes of len 1 is not good enough:
Output:
lt1a/forums/3 /home/forums 5> py Python 3.8.10 (default, Nov 14 2022, 12:59:47) [GCC 9.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> b = bytearray() >>> for x in b'abc': ... print(repr(x)) ... b.append(x) ... 97 98 99 >>> b bytearray(b'abc') >>> b.append(100) >>> b.append(b'e') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'bytes' object cannot be interpreted as an integer >>> b.append(bytearray(b'e')) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'bytearray' object cannot be interpreted as an integer >>>
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#14
apparently a byte (no 's') is (equivalent to) an int for bytearray.append() to work with:
Output:
lt1a/forums/3 /home/forums 6> py Python 3.8.10 (default, Nov 14 2022, 12:59:47) [GCC 9.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> b = bytearray() >>> b.extend(b'abc') >>> b bytearray(b'abc') >>>
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#15
I do not understand any of your last 3 posts. Do you have a question?
Reply
#16
(Mar-15-2023, 01:18 AM)deanhystad Wrote: I do not understand any of your last 3 posts. Do you have a question?

i did post a question (post #8). reading answers changes that question, now. how to i extend a bytearray with another bytearray? originally i wanted to use .append() thinking a bytearray was always "one thing" regardless of length and as such did not check for .extend(). now i know i must treat any of the string types as the sequences they are and work it all together that way.

and i realized i could append one (and only one) byte code to a bytearray with .append() by giving it an int.
Output:
lt1a/forums/2 /home/forums 4> py Python 3.8.10 (default, Nov 14 2022, 12:59:47) [GCC 9.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> a=bytearray() >>> a.append(97) >>> a.append(98) >>> a.append(99) >>> a.append(100) >>> a bytearray(b'abcd') >>>
i was also exploring other alternatives (slicing) to achieve the original goal. i found bytes can be extended onto bytearrays as ints. understanding that a bytearray is a sequence of ints will be somewhat confusing since a list of ints is a different thing, yet basically the same but, with a different API.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#17
a.extend(b) works fine if a is a bytearray and b is bytes, a bytearray, or a list of ints.
Skaperen likes this post
Reply
#18
You cannot append int objects to a byte array. You can only append bytes. You can specify a byte using an int value in the range 0 to 255, which makes it look like you can append int objects, but Python only uses the int object to create a byte object.
x = bytearray()
for y in range(100000):
    x.append(y)
Error:
Traceback (most recent call last): File "...test.py", line 3, in <module> x.append(y) ValueError: byte must be in range(0, 256)
Notice the error is from byte, not bytearray.
Reply
#19
(Mar-14-2023, 10:19 PM)Skaperen Wrote: each element from a bytes is a byte, also. it wants ints.
Python documentation Wrote:While bytes literals and representations are based on ASCII text, bytes objects actually behave like immutable sequences of integers, with each value in the sequence restricted such that 0 <= x < 256 (attempts to violate this restriction will trigger ValueError).
Reply
#20
can it be said, when an int is used to be the value of a byte that the int is "coerced" from an int into a byte? can it be thought of that there is a byte type but it is not available directly (limited to internal use) and one must use an int in the context that works with int, and so on?

in the unnamed language i created 3 decades ago, that Python reminds me of, there was a "byte" type in which literals for it could be written like ints (123, 0x7b, etc.). yeah, it did create some ambiguity issues i never got around to resolve (supply chain issues for round tuits).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Value error when converting hex value to bytearray shubhamjainj 7 10,993 Mar-20-2023, 05:30 PM
Last Post: Skaperen
  Split Bytearray into separate Files by Hex delimter lastyle 5 3,251 Mar-09-2023, 07:49 AM
Last Post: bowlofred
  Save multiple Parts of Bytearray to File ? lastyle 1 1,051 Dec-10-2022, 08:09 AM
Last Post: Gribouillis
  bytearray object - why converting to ascii? Chepilo 2 1,955 Nov-21-2022, 07:25 PM
Last Post: Chepilo
  Bytearray substitution Vismuto 1 2,747 Apr-14-2020, 09:18 AM
Last Post: TomToad
  int() function with bytearray() Jona66 1 2,495 Sep-08-2019, 12:41 PM
Last Post: ichabod801
  Conversion needed from bytearray to Floating point braveYug 1 4,283 May-07-2018, 12:23 PM
Last Post: snippsat
  Windows DIB format in bytearray to image? dusca 2 2,886 Mar-28-2018, 10:35 PM
Last Post: dusca
  Bytearray questions mattps 2 14,548 Mar-25-2018, 06:54 AM
Last Post: mattps
  ByteArray outside while true Schampbakken 5 4,675 Feb-18-2018, 02:02 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020