Bytearray() Function In Python

Function Name:

bytearray

 

Function Signature:

class bytearray([source[, encoding[, errors]]])

 

Function Overview:

 

  • The bytearray() is a constructor to the class bytearray.

 

  • The bytearray() function constructs and returns the bytearray objects.

 

  • Byte arrays are objects in python.

 

  • A bytearray in python is a mutable sequence.

 

Ways to construct a byte array using the bytearray function:

 

1)    Using a string as a source for the bytearray

  • A string is nothing but a collection of characters and each character of the string is represented by a numeric value.

 

  • This numeric value of the corresponding character varies depends upon the encoding.

  

  • By specifying the encoding of a string - that is the mapping between the characters of a string and the numeric values representing the character - the string can be translated into a byte array.

 

Example: String to Byte Array

# Start with first three characters of Japanese Katakana syllabary

c1 = chr(0x30a0)

c2 = chr(0x30a1)

c3 = chr(0x30a2)

 

# Compose these characters into a string

strVal = "{}{}{}".format(c1, c2, c3)

print(strVal)

 

# Get a bytearray from the string

byteCol = bytearray(strVal,"utf-8")

for byte in byteCol:

    print(byte)

 

# Reconstruct the string from the byte array   

print(byteCol.decode("utf-8"))

 

Output:

゠ァア

227

130

160

227

130

161

227

130

162

゠ァア

 

Note that, in UTF-8, if the numeric value or code point for the character is greater than or equal to 128, the character is represented by two to four bytes – each byte taking a value from 128 to 255.

 

This is the reason the output has 12 bytes each 3 bytes corresponding to one katakana character.

 

2)    Creating a byte array of length 'n' with elements initialized to null bytes

At times we need a byte array initialized with all null elements by simply specifying the length of the byte array.

 

Example:

import random

 

# Create a byte array of length 8

bufferLength = 8

sampleBuffer = bytearray(bufferLength)

print("Null bytes:")

print(sampleBuffer)

 

# Fill the buffer with random integer values between 97 and 122 - English lower case alphabets

index = 0

while index < bufferLength:

    sampleBuffer[index] = random.randrange(97, 122)

    index += 1

 

print("Modified byte array - raw:")

 

# Print raw buffer

for byte in sampleBuffer:

    print(byte)

 

print("Modified byte array - string:")

# Print the actual characters

for byte in sampleBuffer:

    print(chr(byte))

 

Output:

Null bytes:

bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')

Modified byte array - raw:

109

108

98

109

111

116

104

111

Modified byte array - string:

m

l

b

m

o

t

h

o

 

3. Using an iterable object:

  • A byte array can be created using any iterable object whose elements are between the values 0 and 255.
  • As of Python version 3.6 the values of the elements cannot be less than 0 and greater than 255

Example: Iterable object to Byte Array

# A python iterable object - a list with values between 0 to 255

charList = [97,98,99,100]

 

# Construct a bytearray instance from the iterable object

buffer   = bytearray(charList)

 

# Print the bytearray

print(buffer)

Output:

bytearray(b'abcd')

 

4. An object that supports the buffer protocol 

A byte array can be constructed using any python object supporting the buffer interface as well.


Copyright 2018 © pythontic.com