• Skip to main content
  • Skip to primary sidebar

CodeBlogMoney

Make Money using Coding and Blogging

You are here: Home / Java / Byte Array to String in Java

Byte Array to String in Java

June 10, 2020 by Jimmy

This article explain how to convert byte array to String.

Byte Array to String code

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

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 

Filed Under: Java

Primary Sidebar

Categories

  • Blogging
  • HTML
  • Java
  • JavaScript
  • jQuery
  • JSON
  • MySql
  • Performance
  • PHP
  • Problem Solving
  • Python
  • Testing
  • XML

Copyright © 2021 · Metro Pro on Genesis Framework · WordPress · Log in