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.
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
- UTF-8 encoding
- 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