Java Convert Byte[] array to String Example
1. Introduction
A byte array is an array of bytes. A byte is binary data with 8 bits which represents an integer with values from -128 to 127. A char is a primitive data type which represents a character. The bytes translate to characters based on the character encoding scheme (such as Unicode, UTF-8, UTF-16, and UTF-32). The String class represents character strings. Therefore, we can convert a byte array into a String object based on a character encoding scheme.
Java String class provides several constructors to convert a byte[] into a String. Here is the syntax:
String(byte[] bytes) String(byte[] bytes, int offset, int length) String(byte[] bytes, int offset, int length, String charsetName) String(byte[] bytes, int offset, int length, Charset charset) String(byte[] bytes, String charsetName) String(byte[] bytes, Charset charset)
In this example, I will create a Junit test class to demonstrate how to convert a byte[] into a String using these constructors.
2. Technologies Used
The example code in this article was built and run using:
- Java 1.8.101
- Eclipse Oxygen
- Junit
- Maven 3.3.9
3. Maven Project
3.1 Dependency
Add Junit to the pom.xml.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>java-bytearray</groupId> <artifactId>java-bytearray</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <release>11</release> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> </project>
3.2 Construct String from Byte[]
In this step, I will create a ByteArraytoStringTest class which constructs a String from a byte[] with a different character encoding scheme. It defines two constants:
TEST_BYTES_ARR– abyte[]with four elements: {77, 97, 114, 121}. These bytes translate toMaryin UTF-8, ISO-8859-1, and ASCII; to慍祲in UTF-16LE; and to䵡特in UTF-16 and UTF-16BE.MARY– aStringwith value of “Mary”.
It includes test methods to convert the TEST_BYTES_ARR into a String object based on the Ascii, utf-8, utf-16, utf-32, and iso_8859-1.
ByteArraytoStringTest.java
package org.jcg.zheng;
import static org.junit.Assert.assertEquals;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import org.junit.Test;
public class ByteArrayToStringTest {
private static final String MARY = "Mary";
private byte[] TEST_BYTES_ARR = new byte[] { 77, 97, 114, 121 };
@Test
public void test_default() {
String newString = new String(TEST_BYTES_ARR);
assertEquals(MARY, newString);
}
@Test
public void test_utf8() {
String newString = new String(TEST_BYTES_ARR, StandardCharsets.UTF_8);
assertEquals(MARY, newString);
}
@Test
public void test_ascii() {
String newString = new String(TEST_BYTES_ARR, StandardCharsets.US_ASCII);
assertEquals(MARY, newString);
}
@Test
public void test_iso_8859() {
String stringTextOfByteArray = new String(TEST_BYTES_ARR, StandardCharsets.ISO_8859_1);
assertEquals(MARY, stringTextOfByteArray);
}
@Test
public void test_utf_16() {
String stringTextOfByteArray = new String(TEST_BYTES_ARR, StandardCharsets.UTF_16);
assertEquals("䵡特", stringTextOfByteArray);
}
@Test
public void test_utf16Be() {
String newString = new String(TEST_BYTES_ARR, StandardCharsets.UTF_16BE);
assertEquals("䵡特", newString);
}
@Test
public void test_utf16Le() {
String newString = new String(TEST_BYTES_ARR, StandardCharsets.UTF_16LE);
assertEquals("慍祲", newString);
}
@Test
public void toStringFormat() {
String stringRepresntationOfByteArray = Arrays.toString(TEST_BYTES_ARR);
assertEquals("[77, 97, 114, 121]", stringRepresntationOfByteArray);
}
}
Note: The string representation of a byte array at line 59 is different from the string constructed from a byte[]. The string constructed with the same byte array would be different based on a different character encoding scheme.
4. Demo
I will execute the Junit tests and capture the results.
![Convert Byte[] array to String - Junit Test Results](https://examples.javacodegeeks.com/wp-content/uploads/2019/04/ByteArray.jpg.webp)
5. Java Convert byte[] array to String – Summary
In this example, we demonstrated how to convert a byte[] to String with java.lang.String constructors.
I also demonstrated that Arrays.toString returns a String representation of the byte array, which is different from converting a byte[] into a String object.
6. Download the Source Code
This example consists of a Maven project to convert a byte[] into a String object.
You can download the full source code of this example here: Java Convert Byte[] to String Example

