Tuesday, January 16, 2018

JAVA - How to convert string to byte[] array

For conversion use method java.lang.String.getBytes():
String s = "This is text";
    
char[] bytes = s.getBytes();
    
System.out.println( Arrays.toString( bytes ) );
Output:
[84, 104, 105, 115, 32, 105, 115, 32, 116, 101, 120, 116]
Note: In method java.lang.String.getBytes() you can optionally set default charset.

No comments:

Post a Comment