Home » Core Java » Java Convert Byte[] array to String Example

About Mary Zheng

Mary Zheng
Mary has graduated from Mechanical Engineering department at ShangHai JiaoTong University. She also holds a Master degree in Computer Science from Webster University. During her studies she has been involved with a large number of projects ranging from programming and software engineering. She works as a senior Software Engineer in the telecommunications sector where she acts as a leader and works with others to design, implement, and monitor the software solution.

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.


<project xmlns="http://maven.apache.org/POM/4.0.0"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">





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 – a byte[] with four elements: {77, 97, 114, 121}. These bytes translate to Mary in UTF-8, ISO-8859-1, and ASCII; to 慍祲 in UTF-16LE; and to 䵡特 in UTF-16 and UTF-16BE.
  • MARY – a String with 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.


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 };

    public void test_default() {
        String newString = new String(TEST_BYTES_ARR);
        assertEquals(MARY, newString);

    public void test_utf8() {
        String newString = new String(TEST_BYTES_ARR, StandardCharsets.UTF_8);
        assertEquals(MARY, newString);

    public void test_ascii() {
        String newString = new String(TEST_BYTES_ARR, StandardCharsets.US_ASCII);
        assertEquals(MARY, newString);

    public void test_iso_8859() {
        String stringTextOfByteArray = new String(TEST_BYTES_ARR, StandardCharsets.ISO_8859_1);
        assertEquals(MARY, stringTextOfByteArray);

    public void test_utf_16() {
        String stringTextOfByteArray = new String(TEST_BYTES_ARR, StandardCharsets.UTF_16);
        assertEquals("䵡特", stringTextOfByteArray);

    public void test_utf16Be() {
        String newString = new String(TEST_BYTES_ARR, StandardCharsets.UTF_16BE);
        assertEquals("䵡特", newString);

    public void test_utf16Le() {
        String newString = new String(TEST_BYTES_ARR, StandardCharsets.UTF_16LE);
        assertEquals("慍祲", newString);

    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
Figure 1 Junit Test Results

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
(+2 rating, 2 votes)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area


Leave a Reply

Notify of