AppDividend
Latest Code Tutorials

Python bytearray() Example | bytearray() Function in Python

0

Python bytearray() is an inbuilt method that returns the bytearray object which is an array of the given bytes. The bytearray() method returns the bytearray object, which is an array of given bytes. It gives a mutable sequence of integers in the range 0 <= x < 256.

Python bytearray Example

The bytearray() function returns a bytearray object.

It can convert objects into bytearray objects, or create an empty bytearray object of the specified size.

There are some rules followed by bytearray() function depending on the type of source.

  1. If there is no argument is passed, an empty byte array is returned.
  2. If the source is the integer, it initializes the byte array of given length with null values.
  3. If the source is a string, encoding is mandatory and used to convert string to a byte array.
  4. If the source is iterable, such as list, it must be iterable of the integers in that range of 0 <= x < 256, which are used as initial content of the array.

See the following syntax of the bytearray() function.

bytearray(source, encoding, error)
  1. source (Optional) – It is the source to initialize an array of bytes.
  2. encoding (Optional) – If the source is a string, the encoding of the string.
  3. errors (Optional) – If the source is a string, the action to take when the encoding conversion fails (Read more: String encoding)

The bytearray() method returns the array of bytes of the given size and initialization values.

# app.py

st3 = "Millie is Good Actor"

arr = bytearray(st3, 'utf-8')
print(arr)

See the following output.

➜  pyt python3 app.py
bytearray(b'Millie is Good Actor')
➜  pyt

#Array of bytes of given integer size

Okay, now see the following python code example.

data = 10

array = bytearray(data)
print(array)

See the following output.

➜  pyt python3 app.py
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
➜  pyt

#Array of bytes from an iterable list

Okay, now we will see Python List and see the output.

# app.py

dataList = [11, 21, 19]

array = bytearray(dataList)
print(array)

See the following output.

➜  pyt python3 app.py
bytearray(b'\x0b\x15\x13')
➜  pyt

#Count ByteArray

Okay, we can count the bytearray length using the len() functionSee the following code.

dataList = [11, 21, 19]

array = bytearray(dataList)
print(len(array))

See the following output.

➜  pyt python3 app.py
3
➜  pyt

#bytearray() with integer

See the following program.

data = bytearray(1)
print(data)

See the output.

➜  pyt python3 app.py
bytearray(b'\x00')
➜  pyt

#Convert bytes to string

See the following program.

data = b'El ni\xc3\xb1o come camar\xc3\xb3n'
print(data)

See the following output.

➜  pyt python3 app.py
b'El ni\xc3\xb1o come camar\xc3\xb3n'
➜  pyt

Finally, Python bytearray() Function Example Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.