Wednesday 2 December 2015

How to convert Byte array to String in Java

In our application sometime there is need of byte array. We can use byte array for encryption purpose . We can convert a string to byte array using string getBytes() method. In byte array string become in decryted format . We can make string from byte array using this syntax.

String str = new String(ByteArray);


package com.javaproficiency;

public class StringToBinary {
public static void main(String[] args) {
String IPCode = "java string is good";
//convert string to byte  array
byte [] bytes = IPCode.getBytes();
System.out.println(" string ="+ IPCode);
System.out.println(" byte array ="+ bytes);
//Convert byte array to string 
String str = new String( bytes );
System.out.println(" str ="+  str);
}

}

Output:

 string =java string is good

 byte array =[B@6c267f18

 str =java string is good

4 comments: