How to convert Byte array to String and vice versa in Java

There are some scenarios in which we have the data in the String format and we want to change it to the byte[] or vice versa. In this tutorial, we are gonna cover how to do these conversions.

Conversion from String to byte[]

There are 2 different approaches that we are gonna discuss for this

Using – String.getBytes(…)

There is String.getBytes() which uses the platform default encoding to encode a string to byte[] and other versions String.getBytes(Charset) which converts String to byte[] using the provided charset.

I generally prefer to use the one where we define the charset as it makes it platform-independent and we can be sure that the code is gonna behave in the same manner everywhere.

Using Base64.Decoder

Base64 class is available since Java 8 version.

As you might be aware – Base64 is designed to represent arbitrary sequences of octets in a form that allows the use of both upper- and lowercase letters but that need not be human-readable, while UTF-8 and UTF-16 are ways to encode Unicode text data. So if you need to encode Base64 data as text, Base64 class is the way to go.

Example to convert String to Byte[]
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;

public class ConvertStringToByteArray {
  public static void main(String[] args) {
    // var is the feature of Java 10
    var validString = "Codingeek.com";

    // Using getBytes method
    byte[] usingGetBytes = validString.getBytes(StandardCharsets.UTF_8);

    // USing Base64 Decoder
    // First encode a String to Base64 format
    var base64EncodedString = Base64.getEncoder().encodeToString(validString.getBytes());
    byte[] usingBase64Encoder = Base64.getDecoder().decode(base64EncodedString);

    System.out.println(
        "Are both arrays equal -> " + Arrays.equals(usingGetBytes, usingBase64Encoder));
  }
}
Output:-
Are both arrays equal -> true

Conversion from String to byte[]

There are 2 different approaches that we are gonna discuss for this as well

String constructor – new String(byte[])

String class has a constructor that takes a byte[] as the input parameter and that constructor can be safely used to convert byte[] to String.

Using Base64.Encoder

As we have already discussed regarding the Base64 data above.

  • Base64 class has an encoder class that accepts the byte[] and returns Base64 encoded string.
  • This String then can be converted to byte[] using the Base64.getDecoder() that we discussed above.
  • Then we again use the new String(byte[]) to convert it to proper String.

Example to convert Byte[] to String

import java.util.Base64;

public class ConvertByteArrayToString {
  public static void main(String[] args) {
    // var is the feature of Java 10
    var validString = "Codingeek.com";

    // Using new String(byte[] )
    var byteArray = validString.getBytes();
    System.out.println("Using String constructor -> " + new String(byteArray));

    // When the string is base64 encoded
    var base64EncodedString = Base64.getEncoder().encodeToString(byteArray);
    var base64DecodedByteArray = Base64.getDecoder().decode(base64EncodedString);

    System.out.println("\nBase64 encoded string -> " + base64EncodedString);
    System.out.println("Using with base -> " + new String(base64DecodedByteArray));
  }
}
Output:-
Using String constructor -> Codingeek.com

Base64 encoded string -> Q29kaW5nZWVrLmNvbQ==
Using with base -> Codingeek.com

An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding.
Do not forget to share and Subscribe.
Happy coding!! 😊

Recommended -