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

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.