1

Python bytearray()

 2 years ago
source link: https://dev.to/itsmycode/python-bytearray-219i
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

ItsMyCode |

Python bytearray() function returns a bytearray object that means it converts an object into bytearray objects, which is an array of given bytes.

The bytearray() method provides mutable sequence of objects in the range of 0 <= x < 256

If you want an immutable version, you can use the bytes() method.

bytearray() Syntax

The syntax of the bytearray() method is:

**bytearray([source[, encoding[, errors]]])**
Enter fullscreen modeExit fullscreen mode

bytearray() Parameters

The bytearray() method takes three optional parameters.

  • source (Optional) – Initializes the array of bytes
  • encoding (Optional) – in case if the source is a string, the encoding of the string.
  • errors (Optional) – An action to take if the encoding conversion fails.

The source parameter can be of any below type as follows.

Type Description String Converts the given string into bytes using str.encode(). In the case of a string, you must also pass encoding as an argument and, optionally errors

Integer Creates an array of provided size and initializes with null bytes Object A read-only buffer of the object will be used to initialize the byte array Iterable Creates an array of size equal to the iterable count and initialized to the iterable elements. The iterable should be of integers and the range should be in between 0 <= x < 256

No source (arguments) An array of size 0 is created.

bytearray() Return Value

The bytearray() function returns an array of bytes of the given size.

Example 1: Array of bytes of given integer size

In the case of an integer, it creates an array of provided size and initializes with null bytes.

# size of array
size = 6

# bytearray() will create an array of given size
# and initialize with null bytes
arr = bytearray(size)

print(arr)

Enter fullscreen modeExit fullscreen mode

python

Output

bytearray(b'\x00\x00\x00\x00\x00\x00')
Enter fullscreen modeExit fullscreen mode

Example 2: Array of bytes from a string

Converts the given string into bytes using str.encode(). In the case of a string, you must also pass encoding as an argument and, optionally, errors.

# string declaration
string = "Hello World !!!"

# string with encoding 'utf-8'
arr1 = bytearray(string, 'utf-8')
print(arr1)

# string with encoding 'utf-16'
arr2 = bytearray(string, 'utf-16')
print(arr2)

Enter fullscreen modeExit fullscreen mode

Output

bytearray(b'Hello World !!!')
bytearray(b'\xff\xfeH\x00e\x00l\x00l\x00o\x00 \x00W\x00o\x00r\x00l\x00d\x00 \x00!\x00!\x00!\x00')
Enter fullscreen modeExit fullscreen mode

Example 3: Array of bytes from an iterable list

Creates an array of size equal to the iterable count and initialized to the iterable elements. The iterable should be of integers, and the range should be in between 0 <= x < 256

Note: if you pass an integer value greater than 256, Python will throw *ValueError: byte must be in range(0, 256) *

# list of integers
lst = [1, 2, 3, 4, 5]

# iterable as source
arr = bytearray(lst)

print(arr)
print("Count of bytes:", len(arr))

Enter fullscreen modeExit fullscreen mode

Output

bytearray(b'\x01\x02\x03\x04\x05')
Count of bytes: 5
Enter fullscreen modeExit fullscreen mode

Example 4: If no source is passed to bytearray()

If no source is passed into bytearray(), an array of size 0 is created.

# array of size 0 will be created

# iterable as source
arr = bytearray()

print(arr)

Enter fullscreen modeExit fullscreen mode

Output

bytearray(b'')
Enter fullscreen modeExit fullscreen mode

The post Python bytearray() appeared first on ItsMyCode.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK