Java Program to Convert Byte Array to String

03 May, 2022
A byte is 8 bits of binary data so do byte array is an array of bytes used to store the collection of binary data. There are multiple ways to change byte array to String in Java, you can either use methods from JDK, or you can use open-source complementary APIs like Apache commons and Google Guava. These APIs provide at least two sets of methods to create a String from a byte array; one, which uses default platform encoding, and the other which takes character encoding.
Using UTF-8 encodingUsing String Class Constructor  
Using UTF-8 encoding
Using String Class Constructor  
It’s also one of the best practices for specifying character encoding while converting bytes to the character in any programming language. It might be possible that your byte array contains non-printable ASCII characters. Let’s first see JDK’s way of converting byte[] to a string. Some programmers, also recommend using Charset over String for specifying character encoding, e.g. instead of “UTF-8” use StandardCharsets.UTF_8 mainly to avoid Unsupported Encoding Exception in the worst case.
Case 1: Without character encoding
We can convert the byte array to String for the ASCII character set without even specifying the character encoding. The idea is to pass the byte[] to the string. It is as shown in the below example which is as follows:
Example:
Java
// Java Program to Convert Byte Array to String// Without character encoding  // Importing required classesimport java.io.IOException;import java.util.Arrays;  // Main classclass GFG {    // Main driver method    public static void main(String[] args)        throws IOException    {        // Getting bytes from the custom input string        // using getBytes() method and        // storing it in a byte array        byte[] bytes = "Geeksforgeeks".getBytes();          System.out.println(Arrays.toString(bytes));          // Creating a string from the byte array        // without specifying character encoding        String string = new String(bytes);          // Printing the string        System.out.println(string);    }}
[71, 101, 101, 107, 115, 102, 111, 114, 103, 101, 101, 107, 115]
Geeksforgeeks
Case 2: With character encoding
We know that a byte holds 8 bits, which can have up to 256 distinct values. This works fine for the ASCII character set, where only the first 7 bits are used. For character sets with more than 256 values, we should explicitly specify the encoding, which tells how to encode characters into sequences of bytes. 
Note: Here we are using StandardCharsets.UTF_8 to specify the encoding. Before Java 7, we can use the Charset. for Name(“UTF-8”).
Example 2:
Java
// Java Program to Convert Byte Array to String// With character encoding  // Importing required classesimport java.io.IOException;import java.nio.charset.StandardCharsets;  // Main classclass GFG {      // Main driver method    public static void main(String[] args)        throws IOException    {          // Custom input string is converted into array of        // bytes and stored        byte[] bytes = "Geeksforgeeks".getBytes(            StandardCharsets.UTF_8);          // Creating a string from the byte array with        // "UTF-8" encoding        String string            = new String(bytes, StandardCharsets.UTF_8);          // Print and display the string        System.out.println(string);    }}
Geeksforgeeks
Java
// Java Program to Illustrate Conversion of// Byte Array to String// Using String Class Constructor  // Classclass GFG {      // Main driver method    public static void main(String[] args)    {          // Try block to check for exceptions        try {              // Getting bytes of input byte array            // using getBytes() method            byte[] input = "GeeksforGeeks".getBytes();              // Creating a corresponding string            String str = new String(input);              // Printing the above string            System.out.println(str);        }          // Catch block to handle exceptions        catch (Exception e) {              // Display exceptions along with line number            // using printStackTrace() method            e.printStackTrace();        }    }}
GeeksforGeeks
Conclusion: We should focus on the type of input data when working with conversion between byte[] array and String in Java.
Use String class when you input data is string or text content.
Use Base64 class when you input data in a byte array.
AshokJaiswal
solankimayank
Java-Array-Programs
Picked
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.