Java convert Byte to String
In this post, you will learn how to convert byte to string using the Java programming language.
A byte is a storage unit capable of representing a single character, such as a letter, number, or symbol. The byte is the number of bits used to encode a single character of text in a computer. It is a unit of digital information.
The process of converting a byte array to a String is called decoding. Here, we have used two ways to convert a byte to a string.
- By using String class constructor
- By using UTF-8 encoding
Convert byte to string by using String class constructor
A string is a sequence of characters. A string is an object of class String. Java String Constructors allows us to create and initialize a string object from different type of arguments. String constructors in Java are created using "new" keyword. The given syntax creates an empty string by calling the default constructor.
String s = new String();
We can use the String class constructor with byte[] as the constructor argument to convert a byte array into String.
String str=new String(bytes);
Without character encoding
In the given Java program, we convert byte array to string for the given ASCII character set without even specifying the character encoding.
// Convert a byte array to string in Java
import java.io.IOException;
import java.util.Arrays;
class ByteString
{
public static void main(String[] args) throws IOException
{
byte[] bytes = "eTutorialspoint".getBytes();
// Create a string from the byte array
// without specifying character encoding
String str = new String(bytes);
System.out.println(str);
}
}
Output of the above code:
eTutorialspoint
With character encoding
ASCII is a character encoding that uses numeric codes to represent characters. A byte is a group of 8 bits that can represent 256 unique values, so each byte represents one of these values. For character sets with more than 256 values, we will have to explicitly specify the encoding, which tells how to encode characters into sequences of bytes.
There are following charsets supported by the Java platform-
- StandardCharsets.ISO_8859_1
- StandardCharsets.US_ASCII
- StandardCharsets.UTF_16
- StandardCharsets.UTF_16BE
- StandardCharsets.UTF_16LE
In the given Java program, we have used the StandardCharsets.UTF_8 to specify the encoding.
// Convert a byte array to string in Java
import java.io.IOException;
import java.nio.charset.StandardCharsets;
class ByteString
{
public static void main(String[] args) throws IOException
{
byte[] bytes = "eTutorialspoint".getBytes(StandardCharsets.UTF_8);
// Create a string from the byte array with "UTF-8" encoding
String str = new String(bytes);
System.out.println(str);
}
}
Output of the above code:
eTutorialspoint
Related Articles
Java string split multiple delimitersEnum with values in Java
Convert array to list in Java
Java random number between 1 and 100
Calculating percentage in Java
Multiplication table program in Java
Java dialogue box
Fibonacci series using recursion in Java
Java sum of array
Circular prime in Java
Vowel and Consonant program in Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Java program to check leap year
Java program to find factorial of a number