Java Program to convert byte array to string


In this program, You will learn how to convert byte array to string in Java.


String str1 = new String(byte, "UTF-8");


Example: How to convert byte array to string in Java

Run it Now
import java.io.UnsupportedEncodingException;
import java.util.Scanner;

class Main {
    public static void main(String args[]) throws UnsupportedEncodingException {

        Scanner s = new Scanner(System.in);
        String str;

        System.out.print("Enter a String :");
        str = s.nextLine();
        byte a[] = str.getBytes();

        System.out.print("\nByte Array Value is :");

        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + ",");
        }

        String str1 = new String(a, "UTF-8");
        System.out.println("\nByte Array to String is :" + str1);

    }
}

Output:

Enter a String :ABC
Byte Array Value is :65,66,67,
Byte Array to String is :ABC