Ben Chuanlong Du's Blog

It is never too late to learn.

Convert Between Byte Array and String in Java

In [ ]:
import java.util.Arrays

String to Byte Array

In [4]:
byte[] bytes = "how are you".getBytes();
for(byte b : bytes){
    System.out.println(b);
}
104
111
119
32
97
114
101
32
121
111
117
Out[4]:
null

Byte Array to String

In [5]:
byte[] bytes = "how are you".getBytes();
System.out.println(new String(bytes));
how are you
Out[5]:
null
In [ ]:

Comments