How to Convert Byte Array to String in Java


In this example we will show the method to convert Byte[] array to a String in Java.

Source Code

package com.beginner.examples;

public class ConvertToStringExample1 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		String str = "Hello World!";
		byte[] bytes = str.getBytes();
		String str1 = new String(bytes);
		System.out.println("bytes:" + bytes);
		System.out.println("str1:" + str1);
	}
}

Output:

bytes:[B@15db9742
str1:Hello World!

Tips

In order to convert Byte array into String format correctly, we have to explicitly create a String object and assign the Byte array to it.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments