Convert Bytearray to String in Python

Convert Bytearray to String in Python

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 the bytearray() 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 the bytearray() 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.

Output:

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.

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.

Output:

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.

Output:

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.

Output:

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.

Output:

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.

Output:

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.

Was this post helpful?

Leave a Reply

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