Byte Array to String in Java

Jun 9, 2020

1 min read

Published in

This article explain how to convert byte array to String.

Byte Array to String code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public class ByteArrayToString {
	
	public static void main(String[] args)
    { 
		byte[] byteArray= {67,111,100,101,32,
				           66,108,111,103,32,
				           77,111,110,101,121};
		
		//Convert byte[] to String		
	    String convertedToString = new String(byteArray);  
	    
	    System.out.println("Byte Array to String : " + convertedToString);
    }
}

Output

Byte Array to String : Code Blog Money

String to Byte Array

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public class StringtoByteArray {
	
	public static void main(String[] args)
    { 
		String sampleString = "CodeBlogMoney";
		
		byte[] byteArray = sampleString.getBytes();
		
		System.out.println("Byte Array data :\n");
		
		for(byte b : byteArray) {
			System.out.print(b + " ");
		}
    }
}

Output

Byte Array data :

67 111 100 101 66 108 111 103 77 111 110 101 121 

Sharing is caring!