In this article, we will explore different ways to convert a byte array(byte[]) to a string in java with example programs.
Java String class has a constructor which takes a
byte[]
as argument and returns its string representation. Example,
byte[] bytes = {72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100}; String str = new String(bytes); System.out.println(str); // Hello World
This method will only work if the contents of byte array represent a string underneath.
2. Using StandardCharsets
If the elements of byte[]
are in default character set, then the previous method is useful.
But if they are not, then you need to use StandardCharSets
to convert a byte array to a string using a specified character set.
String class has another constructor which accepts two arguments: a byte[]
and StandardCharSets
.
Below is an example to convert a byte array to a string using UTF-8 charset.
byte[] bytes = {72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100}; String str = new String(bytes, StandardCharsets.UTF_8); System.out.println(str);
Length of string may not be the same as the length of byte array.
3. Using Base64
It might happen that byte[]
contains binary data such as image, audio, video etc.
In such cases, you cannot directly convert it to a string using the methods outlined above and it is recommended to encode the binary data into a suitable format before converting it to a string
For this, you need to use Base64
class introduced in java 8 and its encodeToString()
method which accepts a byte[] argument. Example,
byte[] binaryData = Files.readAllBytes("test.jpeg"); String encodedString = Base64.encodeBase64String(binaryData);
In this example, we read the contents of an image file using Files.readAllBytes()
method into a byte[]
.
This byte[]
is then converted to a string after encoding it to Base64.
If you are using Apache Commons Codec library, then you can use its
org.apache.commons.codec.binary.Base64
class to convert a byte[]
to string with Base64 conversion. Example,
byte[] binaryData = Files.readAllBytes("test.jpeg"); byte[] encodedData = Base64.encodeBase64(binaryData); String dataStr = new String(encodedData);
Note that encodeBase64()
returns another byte[]
array in Base64 encoding.
This byte[]
can be converted to a string using string constructor as explained earlier.
5. Using CharsetDecoder
java.nio.charset.CharsetDecoder
class can also be used to convert a byte[]
to a string using a standard charset.
The idea is to first create an object of CharsetDecoder
and then use its decode()
method to create a CharBuffer
, which represents a buffer of characters.
It can be converted to a string directly with toString()
method. Example,
byte[] byteArr = {72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100}; Charset charSet = Charset.forName("UTF-8"); CharsetDecoder decoder = charSet.newDecoder(); CharBuffer charBuffer = decoder.decode(ByteBuffer.wrap(byteArr)); String str = charBuffer.toString(); System.out.println(str);
Note that decode()
method accepts an argument of type ByteBuffer
.
An object of ByteBuffer
can be created using its static wrap()
method, which accepts a byte[]
argument.
That is all on converting byte[]
array to a string in java.
Hope the article was useful.