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.

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("Byte array value:");

        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 are:" + str1);

    }
}

Output:

Enter a string:Xiith.com
Byte array value:88,105,105,116,104,46,99,111,109,
Byte Array to String are:Xiith.com