-4

I am absolutly new in Python and I have the following question.

From what I read on the documentation declaring a byte array I am not allowed to assign a value that doesn't come from the range 0 to 255.

Infact doing something like this:

data = bytearray(1000)

for i in range(len(data)):
    data[i] = 10 - i

for b in data:
    print(hex(b))

I am obtaining the following exception:

Traceback (most recent call last):
  File "main.py", line 4, in <module>
    data[i] = 10 - i
ValueError: byte must be in range(0, 256)

So first question: what exactly does it mean? It means that I can declare an array of bytes containing at most 256 bytes? Or am I missing something? If this reasoning is correct: in the case that I have to read a binary file containing more than 256 bytes how can I handle this situation?

Furthermore in another example I found this code snippet used to copy data from a source binary file to a destination one:

from os import strerror

srcname = input("Source file name?: ")
try:
    src = open(srcname, 'rb')
except IOError as e:
    print("Cannot open source file: ", strerror(e.errno))
    exit(e.errno)   
dstname = input("Destination file name?: ")
try:
    dst = open(dstname, 'wb')
except Exception as e:
    print("Cannot create destination file: ", strerr(e.errno))
    src.close()
    exit(e.errno)   

buffer = bytearray(65536)
total  = 0
try:
    readin = src.readinto(buffer)
    while readin > 0:
        written = dst.write(buffer[:readin])
        total += written
        readin = src.readinto(buffer)
except IOError as e:
    print("Cannot create destination file: ", strerr(e.errno))
    exit(e.errno)   

print(total,'byte(s) succesfully written')
src.close()
dst.close()

As you can see it is declaring a bytearray containing more than 255 element:

buffer = bytearray(65536)

I think that I am missing something. How exactly does it work?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • 1
    It only means that each element in the array is a byte — represented as integers you can only have values 0 - 255. `data[i] = 255`? no problem `data[i] = 256`? error. – Mark Jan 02 '20 at 00:16
  • 1
    No, the byte array can be as large as memory allows. Each byte is 8 bits which can represent positive integers from 0-255. – juanpa.arrivillaga Jan 02 '20 at 00:18
  • 1
    Does the second example add anything that the first doesn't? I mean you've already done `bytearray(1000)` in the first example. I bring it up because it's a big chunk of the question, but if it doesn't add anything, you could remove it. – wjandrea Jan 02 '20 at 00:20
  • Documentation: [bytearray](https://docs.python.org/3/library/stdtypes.html#bytearray-objects) - it doesn't specifically mention the [0, 256] range limit, but the [bytes](https://docs.python.org/3/library/stdtypes.html#bytes-objects) documentation does: *"each value in the sequence restricted such that 0 <= x < 256"*. – wjandrea Jan 02 '20 at 00:24

1 Answers1

2

The message is pretty clear: each value in the array must be a byte, and a "byte must be in range(0, 256)". It says nothing about how many elements can be in the array.

user2357112
  • 260,549
  • 28
  • 431
  • 505
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • And if array size / memory were the problem the error would likely be thrown at instantiation: `data = bytearray(1000)`... – Julien Jan 02 '20 at 00:23