Java Convert Byte Array to String

Tags: Byte Array

In this Java tutorial we learn how to convert a byte[] array into String value in Java program.

How to convert byte[] array to String in Java

In Java we can use the constructor of String class to instantiate a String from a given byte array.

byte[] byteData = new byte[] {83, 105, 109, 112, 108, 101, 32, 83, 111, 108, 117, 116, 105, 111, 110};
String stringFromBytes = new String(byteData, StandardCharsets.UTF_8);

The following Java example program to show you in detail how to use String constructor to convert byte array to String value.

ByteArrayToStringExample1.java

import java.nio.charset.StandardCharsets;

public class ByteArrayToStringExample1 {
    public static void main(String... args) {

        byte[] byteData = new byte[] {83, 105, 109, 112, 108, 101, 32, 83, 111, 108, 117, 116, 105, 111, 110};

        // Convert byte array to String
        String stringFromBytes = new String(byteData, StandardCharsets.UTF_8);

        System.out.println("Bytes Data:");
        for(byte b: byteData) {
            System.out.print(b);
        }
        System.out.println("\nString Data:" + stringFromBytes);
    }
}
The output as below.
Bytes Data:
831051091121081013283111108117116105111110
String Data:Simple Solution

Happy Coding 😊

Java Convert String to Byte Array

Java Encode String to Base64 String