How to Convert Byte Array to String in Java
This tutorial demonstrates how to convert a byte[]
to String.
Encoding this String
into a sequence of bytes using the given Charset
, storing the result into a new byte
array.
byte[] byteArray = "this is a string".getBytes(StandardCharsets.UTF_8);
Constructs a new String
by decoding the specified array of bytes using the specified Charset
. The length of the new String
is a function of the charset, and hence may not be equal to the length of the byte array.
String converted = new String(byteArray, StandardCharsets.UTF_8);
Note: These methods always replaces malformed-input and unmappable-character sequences with this charset’s default replacement byte array. The CharsetEncoder
class should be used when more control over the encoding process is required.
Convert Byte Array to String and Vise Versa
The following application demonstrates how to convert a string to byte[]
using a specified encoding. Choosing the right encoding is important.
package com.memorynotfound.string;
import java.nio.charset.StandardCharsets;
public class ConvertByteArrayToString {
public static void main(String... args){
String example = "Tĥïŝ ĩš â fůňķŷ Šťŕĭńġ";
System.out.println("Text : " + example);
System.out.println("- - - - - - - - - - - - - - - - - - -");
byte[] utf8ByteArray = example.getBytes(StandardCharsets.UTF_8);
System.out.println("UTF8 String [Byte Array] : " + utf8ByteArray.toString());
String utf8Converted = new String(utf8ByteArray, StandardCharsets.UTF_8);
System.out.println("UTF8 String Converted : " + utf8Converted);
System.out.println("- - - - - - - - - - - - - - - - - - -");
byte[] isoByteArray = example.getBytes(StandardCharsets.ISO_8859_1);
System.out.println("ISO_8859_1 String [Byte Array] : " + isoByteArray.toString());
String i = new String(isoByteArray, StandardCharsets.ISO_8859_1);
System.out.println("ISO_8859_1 String Converted : " + i);
}
}
Output
Choosing the right encoding is very important. As you can see in the first example we choose UTF-8
encoding which is able to encode and decode the string to and from byte[]
without loosing any information. In the second example we use ISO-8859-1
encoding which has a smaller character set as UTF-8
. As a result you experience that some characters may be lost during conversion.
Text : Tĥïŝ ĩš â fůňķŷ Šťŕĭńġ
- - - - - - - - - - - - - - - - - - -
UTF8 String [Byte Array] : [B@5d22bbb7
UTF8 String Converted : Tĥïŝ ĩš â fůňķŷ Šťŕĭńġ
- - - - - - - - - - - - - - - - - - -
ISO_8859_1 String [Byte Array] : [B@39ed3c8d
ISO_8859_1 String Converted : T?ï? ?? â f???? ??????