Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction 
2.
How to convert byte array to String in Java
2.1.
Method 1: UTF-8 encoding
2.1.1.
1. Without using character encoding 
2.1.2.
2. Using character encoding 
2.2.
Method 2: String Class Constructor 
2.2.1.
1. String constructor – new String(byte[]) 
2.2.2.
2. Base64 class 
3.
Frequently Asked Questions 
3.1.
In Java, is it possible to convert byte array to String?
3.2.
Is a byte array equivalent to a binary?
3.3.
How do you convert a byte array to a string in go?
4.
Conclusion 
Last Updated: Mar 28, 2024
Medium

How to Convert byte Array to String in Java

Introduction 

Java's byte data type is an 8-bit signed integer that uses the two's complement technique. Each byte has 8 bits of binary data. An array of bytes is called a byte array. A byte array can be used to store a collection of binary data. 

convert byte array to string

 

Also see, Morris Traversal for Inorder and Rabin Karp Algorithm

How to convert byte array to String in Java

A string and byte[ ] in Java contains textual and binary data. Decoding is the process of transforming a byte array into a String.

Methods to Convert Byte Array to String

  1. UTF-8 encoding
  2. String Class Constructor  

Method 1: UTF-8 encoding

This could be the ideal method for providing character encoding when we convert byte[ ] array to string.

1. Without using character encoding 

By giving the byte array to string constructor, it is possible to convert the byte array to string for the ASCII character set without specifying the character encoding.

Program: 

public class test
{
    public static void main(String[] args)
    {
        byte[] a = "Test Sample".getBytes();
        String s = new String(a);
        System.out.println(s);
    }
}

 

Output:

Test Sample


2. Using character encoding 

While the string is textual data, bytes are binary data. A byte can have 8 bits, each of which has a range of 256 possible values. We do not receive the original string back when we use a different character encoding. The solution is to provide "UTF-8" as a character encoding when we convert byte array to string.

Program1: 

import java.nio.charset.StandardCharsets; 
public class test { 
    public static void main(String[] args)   {    
        byte[] a = "Test Sample".getBytes(StandardCharsets.UTF_8);   
        String string = new String(a, StandardCharsets.UTF_8); 
        System.out.println(string);  
    } 
}

 

Output:

Test Sample

 

Program2:

public class run { 
    public static void main(String[] args)   { 
        byte[] r = { 'T', 'E', 'S', 'T', 'S', 'A', 'M', 'P', 'L', 'E'};
        byte[] d = {82, 97, 110, 100, 111, 109};  
        String x = new String(r);           
        String y = new String(d); 
        System.out.println(x); 
        System.out.println(y); 
    } 
}  

 

Output:

TESTSAMPLE
Random

 

In the byte array r, the character "T" is changed to 84, and so on; in byte array d, 82 is converted to its respective decimal code.

Read More About, Data Structures and Algorithms and Application of Graph in Data Structure

Method 2: String Class Constructor 

When we have to convert byte array to string in Java, we need to pay attention to the input data type.

  • Use String class 
  • Use Base64 class
     

1. String constructor – new String(byte[]) 

By passing byte[] as the constructor argument, we can convert byte array to string. This technique is solely used when the input is a string or text.

Program: 

public class test{
    public static void main(String[] args) {
        byte[] a = "Test Sample".getBytes(); 
        String x = new String(a);
        System.out.println(x);
    }
}

 

Output:

Test Sample 


2. Base64 class 

If your input data is in a byte array, Base64 is an excellent method to use to convert byte  array to string.

Program: 

public class name {
   public static void main(String args[]) {
      byte[] a= new byte[] {60, 60, 32, 82, 105, 97, 32, 62, 62};
      String x= new String(a);
      System.out.println(" "+x);
   }
}

 

Output:

<< Ria >>

Also see, Longest Common Substring

Frequently Asked Questions 

In Java, is it possible to convert byte array to String?

Yes. To convert a byte value to a string type in Java, we need a given byte value.

Is a byte array equivalent to a binary?

Most of the data in byte arrays are binary. None of the text encodings will function if the byte array you are attempting to convert to a String contains binary data.

How do you convert a byte array to a string in go?

In Go, you can convert a byte array to a string using the string() function. This function takes a byte array as an argument and returns a string. You can also use the fmt. Sprintf() function to convert a byte array to a string.

Conclusion 

In this blog, we have discussed how to convert byte array to string.

If you think this blog has helped you enhance your knowledge about the above question, and if you want to learn more, check out our articles. Visit our website to read more such blogs. 

Related Article:


Check out the following problems - 

But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundles for placement preparations. Enroll in our courses and refer to the mock test and problems available; look at the Problem Sheets interview experiences, and interview bundle for placement preparations. You can also book an interview session with us.  

However, you may consider our paid courses to give your career an edge over others!

Live masterclass