How To Convert Byte[] Array To String in Java
It is common to convert a String into a byte[] array and back again. The catch is that how you convert depends on what the bytes actually are: human-readable text in some encoding, or arbitrary binary data such as the output of a cipher. Using the wrong approach silently corrupts your data.
Note: This post has been updated to always specify a charset explicitly and to cover the binary-data case. The original advice —
new String(bytes)— relies on the platform’s default charset, which makes the result differ from machine to machine, and it cannot round-trip binary data at all. Both pitfalls are addressed below.
Why toString() Does Not Work
The obvious-looking call does not give you the text back:
String s = bytes.toString();
bytes is an array, and arrays inherit Object.toString(), which returns the class name plus the identity hash code ([B@70fccc53) — not the contents. There is no overload to do what you want, so you have to build the String yourself.
Text Bytes: Specify the Charset
When the bytes really are encoded text, hand the String constructor an explicit charset so the result is the same everywhere:
import java.nio.charset.StandardCharsets;
byte[] bytes = example.getBytes(StandardCharsets.UTF_8);
String s = new String(bytes, StandardCharsets.UTF_8);
The key is that the charset used to encode (getBytes) and to decode (new String) must match. Plain new String(bytes) and getBytes() use the JVM’s default charset instead, so code that works on your machine can break on another. (Java 18+ defaults to UTF-8, but being explicit is still the only portable choice.)
Here is a complete example:
import java.nio.charset.StandardCharsets;
public class TestByte {
public static void main(String[] argv) {
String example = "This is an example";
byte[] bytes = example.getBytes(StandardCharsets.UTF_8);
System.out.println("Text : " + example);
System.out.println("Text [Byte Format] : " + bytes);
System.out.println("Text [Byte Format] : " + bytes.toString());
String s = new String(bytes, StandardCharsets.UTF_8);
System.out.println("Text Decoded : " + s);
}
}
Output
Text : This is an example
Text [Byte Format] : [B@70fccc53
Text [Byte Format] : [B@70fccc53
Text Decoded : This is an example
Binary Bytes: Use Base64
For arbitrary binary data — encryption or compression output, image bytes, and so on — a charset is the wrong tool. Not every byte sequence is valid in a given encoding, so new String(bytes, ...) replaces the invalid bytes and you lose data. Encode such bytes with Base64 instead, which maps any byte sequence to safe ASCII characters and back:
import java.util.Base64;
byte[] data = cipher.doFinal(plaintext); // arbitrary binary
String encoded = Base64.getEncoder().encodeToString(data);
byte[] decoded = Base64.getDecoder().decode(encoded); // identical to data
So the rule of thumb is: encoded text → a charset; raw bytes → Base64.
The Disqus comment system is loading ...
If the message does not appear, please check your Disqus configuration.