How To Convert A Python ByteArray Object To String, Bytes, Int, List With Examples

In the previous article, I have told you what is python bytearray object, how to create, modify, and append it. In this article, I will show you some examples about how to convert a bytearray object to string, bytes, integer, and list object in python source code.

1. Python ByteArray To String Example.

1.1 Example 1.

  1. In Python, you can convert a bytearray object to a string using the decode() method.
  2. The decode() method takes an optional argument that specifies the character encoding of the bytearray.
  3. If no argument is provided, the default encoding is 'utf-8'.
  4. Here’s an example of how to convert a bytearray to a string:
    # Create a bytearray from a string
    my_string = "hello world"
    my_bytearray = bytearray(my_string, "utf-8")
    
    # Print the bytearray object on the console
    print(my_bytearray)
    
    # Change the first byte ascii code to 72 which represents character H.
    my_bytearray[0] = 72
    
    
    # Convert the bytearray to a string
    my_string = my_bytearray.decode()
    
    # Print the final string
    print(my_string)   # Output: hello world
  5. Below is the above source code executing output.
    bytearray(b'hello world')
    Hello world
    >>>
  6. In this example, a bytearray object is created from a string using the bytearray() function with the second argument "utf-8".
  7. The decode() method is then called on the bytearray object to convert it to a string.
  8. Since the bytearray was created with the same encoding as the original string, the resulting string is identical to the original. Finally, the completed string is printed to the console.

1.2 Example 2.

  1. If the bytearray was created with a different encoding than the default, you can specify the correct encoding as an argument to the decode() method.
  2. Here’s an example of how to do this:
    # Create a bytearray from a string with a different encoding
    my_string = "你好, world"
    # Use GB2312 text encoding.
    my_bytearray = bytearray(my_string, "GB2312")
    
    # Print the bytearray object on the console
    print(my_bytearray)
    
    # Convert the bytearray to a string with the correct encoding
    my_string = my_bytearray.decode("GB2312")
    
    # Print the final string
    print(my_string)
  3. Below is the output of the above example code.
    bytearray(b'\xc4\xe3\xba\xc3, world')
    你好, world

2. Python ByteArray To Bytes Example.

  1. In Python, you can convert a bytearray object to bytes using the bytes() function.
  2. The bytes() function returns an immutable bytes object that contains the same elements as the bytearray.
  3. Here’s an example of how to convert a bytearray to bytes:
    # Create a bytearray from a string
    my_string = "hello world"
    my_bytearray = bytearray(my_string, "utf-8")
    
    # Convert the bytearray to bytes
    my_bytes = bytes(my_bytearray)
    
    # Print the final bytes object
    print(my_bytes)   # Output: b'hello world'
  4. Below is the output of the above code.
    b'hello world'
  5. In this example, a bytearray object is created from a string using the bytearray() function with the second argument "utf-8". The bytes() function is then called with the bytearray object as an argument to convert it to an immutable bytes object.
  6. Since the bytes object is immutable, you cannot modify its elements like you can with a bytearray.
  7. However, you can access individual elements of the bytes object using indexing, just like with a bytearray.
  8. Here’s an example of how to access individual elements of a bytes object:
    # Create a bytes object from a bytearray
    my_bytearray = bytearray(b"hello world")
    my_bytes = bytes(my_bytearray)
    # Access an individual element of the bytes object
    print(my_bytes[0])   # Output: 104
  9. Below is the output.
    104
  10. In this example, a bytearray object is created using the bytearray() function with the argument b"hello world".
  11. The bytes() function is then called with the bytearray object as an argument to convert it to an immutable bytes object.
  12. Finally, the first element of the bytes object is accessed using indexing, and its value, which is the ASCII code for ‘h’, is printed to the console.

3. Python ByteArray To Integer Example.

  1. In Python, you can convert a bytearray object to an integer using the int.from_bytes() method.
  2. The int.from_bytes() method takes two arguments: the bytearray object to convert, and the byte order of the integer representation.
  3. Here’s an example of how to convert a bytearray to an integer:
    # Create a bytearray from an integer
    my_int = 1000
    my_bytearray = my_int.to_bytes(2, byteorder="big")
    # Convert the bytearray to an integer
    my_int = int.from_bytes(my_bytearray, byteorder="big")
    # Print the final integer
    print(my_int)   # Output: 1000
  4. In this example, an integer is converted to a bytearray using the to_bytes() method with a length of 2 and a byte order of “big”.
  5. The int.from_bytes() method is then called on the bytearray object to convert it back to an integer with the same value. Finally, the completed integer is printed to the console.
  6. If the byte order of the bytearray is little-endian rather than big-endian, you can specify this as the byte order in the int.from_bytes() method.
  7. Here’s an example of how to do this:
    # Create a bytearray from an integer with little-endian byte order
    my_int = 1000
    my_bytearray = my_int.to_bytes(2, byteorder="little")
    
    # Convert the bytearray to an integer with little-endian byte order
    my_int = int.from_bytes(my_bytearray, byteorder="little")
    
    # Print the final integer
    print(my_int)   # Output: 1000
  8. In this example, an integer is converted to a bytearray using the to_bytes() method with a length of 2 and a byte order of “little“.
  9. The int.from_bytes() method is then called on the bytearray object with the byte order set to “little” to convert it back to an integer with the same value.
  10. Finally, the completed integer is printed to the console.

4. Python ByteArray To List Example.

  1. In Python, you can convert a bytearray object to a list of integers using a list comprehension.
  2. The list comprehension iterates over each element of the bytearray and converts it to an integer.
  3. Here’s an example of how to convert a bytearray to a list:
    # Create a bytearray from a string
    my_string = "hello world"
    my_bytearray = bytearray(my_string, "utf-8")
    # Convert the bytearray to a list of integers
    my_list = [int(byte) for byte in my_bytearray]
    
    # Print the final list
    print(my_list)   # Output: [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
  4. In this example, a bytearray object is created from a string using the bytearray() function with the second argument "utf-8".
  5. A list comprehension is then used to iterate over each element of the bytearray and convert it to an integer using the int() function.
  6. The resulting list contains the integers corresponding to the ASCII codes of the characters in the original string.
  7. Finally, the completed list is printed to the console.
  8. If you want to convert the bytearray to a list of characters instead of a list of integers, you can use the chr() function instead of the int() function in the list comprehension.
  9. Here’s an example of how to do this:
    # Create a bytearray from a string
    my_string = "hello world"
    my_bytearray = bytearray(my_string, "utf-8")
    
    # Convert the bytearray to a list of characters
    my_list = [chr(byte) for byte in my_bytearray]
    
    # Print the final list
    print(my_list)   # Output: ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']

References

  1. How To Create, Modify, Append ByteArray Object In Python.

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.