Python byte array to string array

Python Convert Bytes to String

You can convert bytes to strings very easily in Python by using the decode() or str() function. Bytes and strings are two data types and they play a crucial role in many applications. However, in some cases, we may need to convert a byte string to a regular string. In this article, we will learn different methods to convert bytes to a string in Python.

The quick answer to convert bytes to a string in python is to use the decode() or str() function. But there is details to that which we will cover in this article. So let’s get started.

1. Quick Examples of Converting Bytes to String

Below are the most common methods for converting bytes to a string. In the following sections, we will discuss each of these methods in detail with examples.

 # Quick examples of converting bytes to string # Create byte b = b'sparkByExamples' # Using the decode() method s = b.decode('utf-8') # Using the str() constructor s = str(b) # Using the bytes.decode() method s = bytes.decode(b, 'utf-8') # Using the bytearray.decode() method s = b.decode('utf-8') # Using the codecs.decode() method import codecs string = codecs.decode(b, 'utf-8') 

2. What are Bytes in Python?

In Python, bytes is a built-in data type that represents a sequence of bytes. A sequence of bytes can represent any kind of data, including text, images, video, and other types of binary data.

Bytes can represent non-ASCII characters, such as emoji. Because emoji and other non-ASCII characters are represented using multiple bytes, they can be difficult to work with when using regular strings.

 # Byte string with an emoji character a byte_string = b'sparkbyexamples \xF0\x9F\x92\x93' # Print the byte string print(byte_string) # Decode the byte string to a regular string string = byte_string.decode('utf-8') # Print the regular string print(string) 

Yields, the following output:

 # Output: b'sparkbyexamples \xf0\x9f\x92\x93' sparkbyexamples 💓 (love emoji) 

3. Convert Bytes to String using decode()

To convert bytes to strings in python, use the decode() function. decode() is built-in python method used to convert bytes to strings. To use the decode() method, you need to specify the character encoding that was used to encode the byte string.

Читайте также:  Php header new line

This method is designed specifically to decode byte strings into regular strings and takes into account the character encoding used to encode the byte string.

 # Create a byte string using utf-8 encoding byte_string = b'sparkbyexamples' # Use utf-8 to decode() the string string = byte_string.decode('utf-8') # Print the regular string print(string) # Output: # sparkbyexamples 

Below is another example that uses UTF-16 encoding:

 # Create a byte string using utf-16 encoding byte_string = b'\xff\xfes\x00p\x00a\x00r\x00k\x00b\x00y\x00e\x00x\x00a\x00m\x00p\x00l\x00e\x00s\x00' # Decode the byte string to a regular string string = byte_string.decode('utf-16') # Print the regular string print(string) # Output: # sparkbyexaples 

Steps to convert bytes to a string using the decode() function in Python:

  1. Find the bytes that you want to convert
  2. Call the decode() method on the byte string and pass the appropriate encoding as an argument.
  3. Assign the decoded string to a variable.
  4. Use the decoded string in your Python code as needed.

You can convert that string back to bytes with UTF-16 encoding using the encode() method:

 # Define a string string = 'sparksbyexamples' # Encode the string to bytes in UTF-16 byte_string = string.encode('utf-16') # Print the byte string print(byte_string) 

4. str() – Bytes to String in Python

str() is a built-in Python function that can be used to convert a bytes object to a string object, by passing the bytes object as the first argument and the desired encoding as the second argument.

Syntax of str() for bytes to string conversion:

 # Syntax of str() str(byte_string, 'utf-8') 

Converting bytes with UTF-8 encoding using the str() function:

 # Create a byte string with a non-ASCII character byte_string = b'sparkbyexamples is \xf0\x9f\x92\x93' # Convert byte string to string using UTF-8 encoding string_utf8 = str(byte_string, 'utf-8') print(string_utf8) # Output: # sparkbyexamples is 💓 (love emoji) 

Byte with a smile emoji using UTF-16 encoding to a string:

 # smile emoji - UTF-16 encoding byte_string = b'\xff\xfe(\x00?\x00?\x00)\x00' # Convert byte string to string string_utf16 = byte_string.decode('utf-16') print(string_utf16) # Output: # 😊 (smile emoji) 

5. Convert Bytes Array to String

To convert a byte array to a string, you can use the bytes() constructor to create a bytes object from the array, and then use the decode() method to convert the bytes object to a string.

 # Create a byte array byte_array = bytearray([115, 112, 97, 114, 107]) # Convert byte array to string using the UTF-8 encoding string_utf8 = bytes(byte_array).decode('utf-8') # Print the string print(string_utf8) # Output: # spark 

6. decode() vs str() for Byte Conversion

The decode() method is used to convert a bytes object to a string by decoding it with a specified character encoding. While the str() constructor is a simpler method that can be used to create a string object directly from a bytes object.

Читайте также:  Background change transition css

Below is the table, showing the major differences between the decode() and str() for conversion of bytes to string:

Источник

Convert Bytearray to String in Python

Convert Bytearray to String in Python

  1. Convert bytearray to string With the bytes() Function in Python
  2. Convert bytearray to string With the bytearray.decode() Function in Python

You can use two primary methods to convert a bytearray into a String in Python: bytes() and bytearray.decode() . In this tutorial, we’ll show you how you can use these functions as methods for this special conversion.

Convert bytearray to string With the bytes() Function in Python

If we have a bytearray containing string characters with the utf-8 encoding and want to convert that array into a string variable, we can use the built-in bytes() function in Python.

The bytes() function returns an immutable bytes object that can then be stored inside a string variable. The following code snippet demonstrates how we can convert a bytearray to a string with the bytes() function.

b = bytearray("test", encoding="utf-8") str1 = bytes(b) print(str1) 

We converted the bytearray object b into a string variable str1 with the bytes() function in the code above. First, we encoded the text test with a utf-8 encoding inside an object of bytearray . We then converted the bytearray to string with the bytes() function and stored the result inside the string variable str1 .

In the end, we printed the data inside the str1 variable. The output shows that this process adds a b object at the start of our original data and then encloses the data inside single quotes. This problem is addressed in the method discussed next.

Читайте также:  OpenFile test

Convert bytearray to string With the bytearray.decode() Function in Python

As we can see, the bytes() function converts a bytearray to a string but adds additional data to the original string. This problem can be solved by string manipulation, but it is a cumbersome process. The bytearray.decode() function automatically does that for us. This method decodes the data originally encoded inside the bytearray .

The following code snippet demonstrates how we can convert a bytearray to string with the bytearray.decode() function.

b = bytearray("test", encoding="utf-8") str1 = b.decode() print(str1) 

We converted the bytearray object b into a string variable str1 with the b.decode() function in the code above. First, we encoded the text test with a utf-8 encoding inside an object of bytearray . We then converted the bytearray to string with the b.decode() function and stored the result inside the string variable str1 . In the end, we printed the data inside the str1 variable. The output shows that this process does not add any additional data to our originally encoded data.

From the demonstrations above, it is clear that the bytearray.decode() method is far superior to the byte() method for converting a bytearray object into a string variable.

Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.

Related Article — Python String

Источник

Оцените статью