Signup/Sign In
PUBLISHED ON: MARCH 5, 2021

How to convert bytes to string in Java

In the python programming language, bytes are just like an array. When we want to represent a group of byte values then we can consider bytes data types. The bytes data types allow values only from 0 to 255. The bytes data types are immutable. Even when no parameters passed to bytes, it returns an array of size zero.

In this tutorial, we will learn how to convert bytes to string using the decode() method, codecs module and str() method. The decode() method is a string method and this method is used to decode the encoded string which returns the original string. It works exactly opposite to the encode() method. The codecs module consists of a decode() method that converts the encoded bytes to string and the str() function converts the passed object to a string.

Example 1: Converting Byte to String

The below example shows how to convert the byte to string using the decode() method.

string_byte="This string will be converted to bytes"
print("String to be converted :",string_byte)
byte_data=bytes(string_byte,"utf-16")
print("string to byte conversion :",byte_data)
byte_string=byte_data.decode("utf-16","strict")
print("byte to string conversion:",byte_string)

In the above code, we stored the string to be converted into the variable string_byte. Using the bytes() method along with the encoding "utf-16", we converted the string to bytes. The converted string is stored in the variable called byte_data. In the next step, we have used the decode() method to decode the bytes object. As we can see from the output, we get the original string by using the decode() method.


String to be converted: This string will be converted to bytes
string to byte conversion : b'\xff\xfeT\x00h\x00i\x00s\x00 \x00s\x00t\x00r\x00i\x00n\x00g\x00 \x00w\x00i\x00l\x00l\x00 \x00c\x00o\x00n\x00v\x00e\x00r\x00t\x00e\x00d\x00 \x00t\x00o\x00 \x00b\x00y\x00t\x00e\x00s\x00'
byte to string conversion: This string will be converted to bytes

Example 2: Converting Byte to String

We can convert the bytes object to a string using the codecs module. In the codecs module, there is a method called decode() which converts the bytes object to a string object.

The below example shows how to convert the byte to string using the codecs module.

import codecs
string_byte="studytonight.com"
print("String to be converted :",string_byte)
byte_data=bytes(string_byte,"utf-8")
print("string to byte conversion :",byte_data)
byte_string=codecs.decode(byte_data)
print("byte to string conversion:",byte_string)

Once we run the program it shows the following result.


String to be converted: studytonight.com
string to byte conversion: b'studytonight.com'
byte to string conversion: studytonight.com

Example 3: Converting Byte to String

We can convert the byte to string by simply using the in-built function str(). We have to pass bytes object and the encoding used to convert the bytes to the str() function.

The below example shows how to convert the bytes object to a string.

#Initializing string
string_byte="studytonight.com"
print("String to be converted :",string_byte)
#converting string to byte
byte_data=bytes(string_byte,"utf-8")
print("string to byte conversion :",byte_data)
#converting byte to string
byte_string=str(byte_data,"utf-8")
print("byte to string conversion:",byte_string)

In the above example, we converted the bytes object to the string object using the str() function. Once we run the program it shows the following result.


String to be converted: studytonight.com
string to byte conversion: b'studytonight.com'
byte to string conversion: studytonight.com

Conclusion

In this tutorial, we have learned how to convert bytes to string using the decode() method, codecs module, and str() function.



About the author:
An enthusiastic fresher, a patient person who loves to work in diverse fields. I am a creative person and always present the work with utmost perfection.