How to Master Python Byte Arrays: Creation, Manipulation, and Beyond

Ever felt confused about Python byte arrays? This video is here to clear the air! We’ll delve into creating and manipulating byte arrays, including how to construct them from lists or strings, modify their elements, reverse their order, and even clear them entirely. With clear examples, you’ll gain a solid understanding of byte arrays and unlock their power for data manipulation.

1. Video.

2. Python Source Code.

"""
  This function demonstrates creating a bytearray in two ways:
  1. From a list of integers representing ASCII codes.
  2. From a string with a specified encoding.
"""
def create_bytearray_example():
  # Create a bytearray from a list of ASCII codes
  ba = bytearray([72, 73, 74, 75])
  print("Created bytearray:", ba)

  # Print each element of the bytearray and its corresponding character
  for el in ba:
    print(el, chr(el))

  # Create a bytearray from a string encoded in UTF-8
  ba1 = bytearray('hello', 'utf-8')
  print("ba1:", ba1)



"""
  This function demonstrates appending elements to a bytearray using append and extend methods.
"""
def append_bytearray_example():
  # Create a bytearray from a string literal (already encoded in bytes)
  ba = bytearray(b"hello")

  # Append the ASCII code for space (32)
  ba.append(32)
  
  # Extend the bytearray with another string literal (already encoded in bytes)
  ba.extend(b"world")
  print("Appended bytearray:", ba)



"""
  This function demonstrates converting a bytearray to a string with a specified encoding.
"""
def to_string_example():

  # Create a bytearray from a string literal (already encoded in bytes)
  ba = bytearray(b"hello world")

  # Create another bytearray from a unicode string with UTF-8 encoding
  ba = bytearray("你好世界", 'utf-8')

  print('ba = ', ba)
  # Decode the bytearray to a string using UTF-8 encoding
  s = ba.decode('utf-8')
  print("Converted to string:", s)



"""
  This function demonstrates reversing the order of elements in a bytearray.
"""
def reverse_bytearray_example():

  # Create a bytearray from a string literal (already encoded in bytes)
  ba = bytearray(b"hello")

  # Reverse the order of elements in the bytearray
  ba.reverse()

  print("Reversed bytearray:", ba)



"""
  This function demonstrates removing all elements from a bytearray.
"""
def clear_bytearray_example():

  # Create a bytearray from a string literal (already encoded in bytes)
  ba = bytearray(b"hello")

  # Remove all elements from the bytearray
  ba.clear()

  print("Cleared bytearray:", ba)



"""
  This function calls all the example functions to demonstrate bytearray operations.
"""
def main():

  #create_bytearray_example()
  #append_bytearray_example()
  to_string_example()
  reverse_bytearray_example()
  clear_bytearray_example()



if __name__ == "__main__":
  main()

Leave a Comment

Your email address will not be published. Required fields are marked *

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