Saturday, June 1, 2013

How To Convert String To Byte Array And Vice Versa In Java ?

How To Convert String To Byte Array In Java ?

The getBytes() method can be used to convert a String into byte array. An Example is given below:

String secret = "The Gold Is In Room 345";
 byte[] secretInBytes=secret.getBytes();


How To Convert Byte array To String In Java?

To convert the string back to byte array use new String(byteArrayName)

byte[] arr ; ----> Suppose 'arr' is a byte array
String str=new String(a); -------> Converts the byte array 'arr' to String

No comments:

Post a Comment