Convert String to byte array in Java
Description
The following code shows how to convert String to byte array.
Example
/*from w w w . j a v a 2 s.c om*/
public class Main {
public static void main(String[] args) {
String stringToConvert = "this is a test from java2s.com";
byte[] theByteArray = stringToConvert.getBytes();
System.out.println(theByteArray.length);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »