Table of Contents
Python supports different types of sequence objects to store data. One such object is a bytearray object. As the name suggests, a bytearray object is an array of bytes or a sequence of bytes. In this article, we will discuss different ways to convert bytearray to string in Python.
How to create a bytearray from a string?
Before converting a bytearray to a string, let us first discuss how we can convert a string into a bytearray. After creating a bytearray object from a string, we will learn how to convert a bytearray to a string in the subsequent sections.
To convert a string to a bytearray we use the bytearray()
constructor. The syntax for the bytearray()
constructor is as follows.
byteArrayObject=bytearray(input_string, encoding_format, error_message)
Here,
- The
byteArrayObject
is the output given by thebytearray()
constructor. input_string
is the string that has to be converted into a bytearray.- ‘
encoding_format
’ is the encoding format that we follow while converting a string to bytearray. Generally, it is ‘utf-8
’ or ‘utf-16
’. error_message
is the message that is displayed when any error occurs during the execution of thebytearray()
constructor. It is an optional argument.
To convert a string to a bytearray object, we can pass the input string and the encoding format to the bytearray constructor as follows.
1 2 3 4 5 6 |
myString = "Java2Blog" byteArrayObject = bytearray(myString, 'utf-8') print("The input string is:", myString) print("The bytearray object is:", byteArrayObject) |
Output:
1 2 3 4 |
The input string is: Java2Blog The bytearray object is: bytearray(b'Java2Blog') |
As you can observe, we have created a bytearray object from a string. Now we will discuss different ways to convert a bytearray to string in python.
Using the str() Function to convert Bytearray to String in Python
We can convert a bytearray to a string using the str()
function. Generally, we use the str()
function to convert an integer, a floating-point number, or other value to a string as follows.
1 2 3 4 |
myFloat = 123.4567 myString = str(myFloat) |
Here, we don’t need to specify the encoding format. But, If we want to convert a bytearray to string using the str()
function, we also need to specify the encoding format as the second argument as shown below.
1 2 3 4 5 6 7 8 |
myString = "Java2Blog" byteArrayObject = bytearray(myString, 'utf-8') print("The input string is:", myString) print("The bytearray object is:", byteArrayObject) output_string = str(byteArrayObject, 'utf-8') print("The output string is:", output_string) |
Output:
1 2 3 4 5 |
The input string is: Java2Blog The bytearray object is: bytearray(b'Java2Blog') The output string is: Java2Blog |
Here, the encoding format should be the same as the format used while creating the bytearray object. If we specify any other encoding format in the str()
function, it will give undesired output. The program may also run into errors. You can observe this in the following example.
1 2 3 4 5 6 7 8 9 |
myString = "Java2Blog" byteArrayObject = bytearray(myString, 'utf-8') print("The input string is:", myString) print("The bytearray object is:", byteArrayObject) output_string = str(byteArrayObject, 'utf-16') print("The output string is:", output_string) |
Output:
1 2 3 4 5 6 7 8 |
The input string is: Java2Blog The bytearray object is: bytearray(b'Java2Blog') Traceback (most recent call last): File "/home/aditya1117/PycharmProjects/pythonProject/webscraping.py", line 5, in <module> output_string = str(byteArrayObject, 'utf-16') UnicodeDecodeError: 'utf-16-le' codec can't decode byte 0x67 in position 8: truncated data |
Here, you can see that the str()
method couldn’t convert a bytearray object to a string because we didn’t pass the encoding format same as the one used while creating the bytearray object.
Using the decode() Function to convert Bytearray to String in Python
An alternative way to convert a bytearray to string is by using the decode()
method. The decode()
method, when invoked on a bytearray object, takes the encoding format as input and returns the output string.
You can convert a bytearray to a string using the decode()
method as follows.
1 2 3 4 5 6 7 8 9 10 |
myString = "Java2Blog" byteArrayObject = bytearray(myString, 'utf-8') print("The input string is:", myString) print("The bytearray object is:", byteArrayObject) output_string = byteArrayObject.decode('utf-8') print("The output string is:", output_string) |
Output:
1 2 3 4 5 |
The input string is: Java2Blog The bytearray object is: bytearray(b'Java2Blog') The output string is: Java2Blog |
Again, you have to specify the same encoding format that was used while creating the bytearray from the string. Otherwise, you will get undesired outputs as shown below.
1 2 3 4 5 6 7 8 |
myString = "Java2Blog" byteArrayObject = bytearray(myString, 'utf-8') print("The input string is:", myString) print("The bytearray object is:", byteArrayObject) output_string = byteArrayObject.decode('utf-16') print("The output string is:", output_string) |
Output:
1 2 3 4 5 6 7 8 |
The input string is: Java2Blog The bytearray object is: bytearray(b'Java2Blog') Traceback (most recent call last): File "/home/aditya1117/PycharmProjects/pythonProject/webscraping.py", line 5, in <module> output_string = byteArrayObject.decode('utf-16') UnicodeDecodeError: 'utf-16-le' codec can't decode byte 0x67 in position 8: truncated data |
Using the codecs Module to convert Bytearray to String in Python
An implementation of the decode()
function is also given in the codecs
module. You can also use the decode()
function defined in the codecs
module to convert a bytearray to a string in python.
Here, the decode()
function takes the bytearray object as its first argument and the encoding format as its second argument. After execution, it returns the output string as shown in the following example.
1 2 3 4 5 6 7 8 9 10 |
import codecs myString = "Java2Blog" byteArrayObject = bytearray(myString, 'utf-8') print("The input string is:", myString) print("The bytearray object is:", byteArrayObject) output_string = codecs.decode(byteArrayObject, 'utf-8') print("The output string is:", output_string) |
Output:
1 2 3 4 5 |
The input string is: Java2Blog The bytearray object is: bytearray(b'Java2Blog') The output string is: Java2Blog |
Conclusion
In this article, we have discussed three ways to convert a bytearray object to a string in python. Here, you can use any approach to perform the operation. However, you should make sure that you choose the encoding format correctly. Otherwise, you won’t be able to get desired output.
That’s all about how to convert Bytearray to String in Python.
I hope you enjoyed reading this article. Happy Learning.