coding made simple with some delicious cake

Java – How to convert a byte array to string

In this post we will show you how to convert a byte array to string in java. Converting a string to a byte array is fairly simple however, vice versa is a little more tricky.

Example: Convert string to byte array:

Converting a string to a byte array is very simple as seen below.

String to byte array

1

byte[]bytes="This is a random string".getBytes();

Example: Convert a byte array to string:

Using the byte array toString() method, will not give you the desired result. This will instead return the byte value as a string. To correctly convert the byte array to a string you will need to do the following: