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.
- In Python, you can convert a
bytearray
object to a string using thedecode()
method. - The
decode()
method takes an optional argument that specifies the character encoding of thebytearray
. - If no argument is provided, the default encoding is
'utf-8'
. - 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
- Below is the above source code executing output.
bytearray(b'hello world') Hello world >>>
- In this example, a
bytearray
object is created from a string using thebytearray()
function with the second argument"utf-8"
. - The
decode()
method is then called on thebytearray
object to convert it to a string. - 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.
- If the
bytearray
was created with a different encoding than the default, you can specify the correct encoding as an argument to thedecode()
method. - 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)
- Below is the output of the above example code.
bytearray(b'\xc4\xe3\xba\xc3, world') 你好, world
2. Python ByteArray To Bytes Example.
- In Python, you can convert a
bytearray
object to bytes using thebytes()
function. - The
bytes()
function returns an immutable bytes object that contains the same elements as thebytearray
. - 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'
- Below is the output of the above code.
b'hello world'
- In this example, a
bytearray
object is created from a string using thebytearray()
function with the second argument"utf-8"
. Thebytes()
function is then called with thebytearray
object as an argument to convert it to an immutable bytes object. - Since the
bytes
object is immutable, you cannot modify its elements like you can with abytearray
. - However, you can access individual elements of the
bytes
object using indexing, just like with abytearray
. - 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
- Below is the output.
104
- In this example, a
bytearray
object is created using thebytearray()
function with the argumentb"hello world"
. - The
bytes()
function is then called with thebytearray
object as an argument to convert it to an immutable bytes object. - 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.
- In Python, you can convert a
bytearray
object to an integer using theint.from_bytes()
method. - The
int.from_bytes()
method takes two arguments: thebytearray
object to convert, and the byte order of the integer representation. - 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
- In this example, an integer is converted to a
bytearray
using theto_bytes()
method with a length of 2 and a byte order of “big”. - The
int.from_bytes()
method is then called on thebytearray
object to convert it back to an integer with the same value. Finally, the completed integer is printed to the console. - If the byte order of the
bytearray
is little-endian rather than big-endian, you can specify this as the byte order in theint.from_bytes()
method. - 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
- In this example, an integer is converted to a
bytearray
using theto_bytes()
method with a length of 2 and a byte order of “little“. - The
int.from_bytes()
method is then called on thebytearray
object with the byte order set to “little” to convert it back to an integer with the same value. - Finally, the completed integer is printed to the console.
4. Python ByteArray To List Example.
- In Python, you can convert a
bytearray
object to a list of integers using a list comprehension. - The list comprehension iterates over each element of the
bytearray
and converts it to an integer. - 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]
- In this example, a
bytearray
object is created from a string using thebytearray()
function with the second argument"utf-8"
. - A list comprehension is then used to iterate over each element of the
bytearray
and convert it to an integer using theint()
function. - The resulting list contains the integers corresponding to the ASCII codes of the characters in the original string.
- Finally, the completed list is printed to the console.
- If you want to convert the
bytearray
to a list of characters instead of a list of integers, you can use thechr()
function instead of theint()
function in the list comprehension. - 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