-->
Skip to main content

Convert to String from Byte Array In Java [2021]

Convert to String from Byte Array In Java [2021]

To convert to String from Byte Array we can use the String class Constructors or Base64 Class method introduced in Java8.
In this tutorial, we will cover how to convert to String from Byte Array in Java using 4 ways. In an earlier post, we have covered how to Convert String to Byte Array in Java.

Convert to String from Byte Array in Java

Convert to String from Byte Array in Java

To get String from Byte Array in Java we will use the String class overloaded constructors and the Base64 class method introduced in Java8.
  1. Using new String(byte[] bytes)
  2. Using new String(byte[] bytes, String charsetName) throws UnsupportedEncodingException
  3. Using new String(byte[] bytes, Charset charset)
  4. Using Base64.getEncoder().encodeToString(byte[] src)

1. Using new String(byte[] bytes)

This constructor directly converts to String from byte[] array.
 // sample string
 String string = "javacodegeek";
 System.out.println(string);

 // convert string to byte array java
 byte[] bytes = string.getBytes();
 System.out.println(Arrays.toString(bytes));

 // get string from byte array java 
 String stringFromByteArray = new String(bytes);
 System.out.println("byte array to string java : " + stringFromByteArray);

Output:

javacodegeek
[106, 97, 118, 97, 99, 111, 100, 101, 103, 101, 101, 107]
byte array to string java : javacodegeek

2. Using new String(byte[] bytes, String charsetName) throws UnsupportedEncodingException

This Constructor directly converts to String from byte[] array using a charset name as a parameter. In this example, we are converting to string from byte[] array using the UTF-8 charset. This Constructor throws UnsupportedEncodingException thus we either need to handle it with try-catch or just throws it.
 // sample string
 String string = "javacodegeek";
 System.out.println(string);

 // convert string to byte array java with charset
 byte[] byteswithcharset = string.getBytes("UTF-8");
 System.out.println(Arrays.toString(byteswithcharset));

 // get string from byte array java with charset
String stringFromByteArrayWithCharset = new String(byteswithcharset, "UTF-8");
System.out.println("byte array to string java using charset : "
+ stringFromByteArrayWithCharset);

Output:

javacodegeek
[106, 97, 118, 97, 99, 111, 100, 101, 103, 101, 101, 107]
byte array to string java using charset : javacodegeek

3. Using new String(byte[] bytes, Charset charset)

This constructor directly converts to  String from byte[] array using Charset.forName(String charset) as the second parameter.
 // sample string
 String string = "javacodegeek";
 System.out.println(string);

 // To see available charsets
 // use Charset.availableCharsets();
		 
 // convert string to byte array java with charset
 byte[] byteswithcharset = string.getBytes(Charset.forName("ISO-8859-1"));
 System.out.println(Arrays.toString(byteswithcharset));

 // get string from byte array java with charset defined
String stringFromByteArrayWithCharset = new String(byteswithcharset, Charset.forName("ISO-8859-1"));
System.out.println("byte array to string java using charset : " +
stringFromByteArrayWithCharset);

Output:

javacodegeek
[106, 97, 118, 97, 99, 111, 100, 101, 103, 101, 101, 107]
byte array to string java using charset : javacodegeek

4. Using Base64.getEncoder().encodeToString(byte[] src)

This method directly converts String from byte[] array using Base64.getEncoder().encodeToString(byte[] src) method.
 // sample string
 String string = "javacodegeek";
 System.out.println(string);

 // convert string to byte array java
 byte[] bytes = Base64.getDecoder().decode(string);
 System.out.println(Arrays.toString(bytes));

 // get string from byte array java using Base64 encodeToString method.
String stringFromByteArrayUsingBase64 = Base64.getEncoder().encodeToString(bytes);
System.out.println("byte array to string java using Base64 : "
+ stringFromByteArrayUsingBase64);

Output:

javacodegeek
[-115, -85, -38, 114, -121, 94, -127, -25, -92]
byte array to string java using Base64 : javacodegeek

Complete Example

Now we will see a complete example using all the three constructors use and the Base64 method use.
 
 import java.io.UnsupportedEncodingException;
 import java.nio.charset.Charset;
 import java.util.Arrays;
 import java.util.Base64;

 public class StringFromByteArrayExample {
   public static void main(String[] args) throws UnsupportedEncodingException {

   // sample string
   String string = "javacodegeek";
   System.out.println("Sample String : " + string);

   // convert string to byte array java
   byte[] bytes = string.getBytes();
   System.out.println("string to byte array : " + Arrays.toString(bytes));

   // convert byte array to string java
String stringFromByteArray = new String(bytes); System.out.println("byte array to string java : "
+ stringFromByteArray); // convert byte array to string java with charset
String stringFromByteArrayWithCharset = new String(bytes, "UTF-8"); System.out.println("byte array to string java using charset : "
+ stringFromByteArrayWithCharset); // convert byte array to string java with charset defined
String stringFromByteArrayWithCharset1 = new String(bytes, Charset.defaultCharset()); System.out.println("byte array to string java using charset : "
+ stringFromByteArrayWithCharset1); byte[] bytesForBase64 = Base64.getDecoder().decode(string); System.out.println("Base64 decoded string to bytes : " + Arrays.toString(bytesForBase64)); // convert byte array to string java using Base64 encodeToString method.
String stringFromByteArrayUsingBase64 = Base64.getEncoder().encodeToString(bytesForBase64); System.out.println("byte array to string java using Base64 : "
+ stringFromByteArrayUsingBase64); } }

Output:

Sample String : javacodegeek
string to byte array : [106, 97, 118, 97, 99, 111, 100, 101, 103, 101, 101, 107]
byte array to string java : javacodegeek
byte array to string java using charset : javacodegeek
byte array to string java using charset : javacodegeek
Base64 decoded string to bytes : [-115, -85, -38, 114, -121, 94, -127, -25, -92]
byte array to string java using Base64 : javacodegeek

Conclusion:

In this tutorial, we have covered how to convert to String from Byte Array in Java using 4 ways
  1. Using new String(byte[] bytes)
  2. Using new String(byte[] bytes, String charsetName) throws UnsupportedEncodingException
  3. Using new String(byte[] bytes, Charset charset)
  4. Using Base64.getEncoder().encodeToString(byte[] src)
Thanks for reading this tutorial so far. If you like this tutorial then please share it with your friends and colleagues. If you have any questions, doubts, suggestions, or feedback then please drop a comment and I'll try to answer your question.

Happy Learning!!!

References:

Comments