-->
Skip to main content

Convert String to Byte Array In Java [2021]

Convert String to Byte Array In Java [2021] 

In this tutorial, we will cover how to convert String to Byte Array in Java using 4 ways.
In the recent tutorial, we have covered how to Convert to String from Byte Array in Java

Convert String to Byte Array In Java

Convert String to Byte Array in Java

To convert String to Byte Array in Java we will use the getBytes() overloaded method in the String class and the Base64 class method introduced in Java8.
  1. Using getBytes()
  2. Using getBytes(String charsetName) throws UnsupportedEncodingException
  3. Using getBytes(Charset charset)
  4. Using Base64.getDecoder().decode(String src)

1. Using getBytes()

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

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

Output:

javacodegeek.com
[106, 97, 118, 97, 99, 111, 100, 101, 103, 101, 101, 107, 46, 99, 111, 109]

2. Using getBytes(String charsetName)

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

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

Output:

javacodegeek.com
[106, 97, 118, 97, 99, 111, 100, 101, 103, 101, 101, 107, 46, 99, 111, 109]

3. Using getBytes(Charset charset)

This method directly converts String to byte[] array using Charset.forName(String charset) method.
 // sample string
 String string = "javacodegeek.com";
 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));

Output:

javacodegeek.com
[106, 97, 118, 97, 99, 111, 100, 101, 103, 101, 101, 107, 46, 99, 111, 109]

4. Using Base64.getDecoder().decode(String src)

This method directly converts String to byte[] array using Base64.getDecoder().decode(String 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)); 

Output:

javacodegeek
[-115, -85, -38, 114, -121, 94, -127, -25, -92]

Complete Example

Now we will see a complete example using all the four methods.
 
 import java.io.UnsupportedEncodingException;
 import java.nio.charset.Charset;
 import java.util.Arrays;
 import java.util.Base64;

 public class StringToByteArrayExample {
   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 string to byte array java with charset
    byte[] byteswithcharset1 = string.getBytes("UTF-8");
    System.out.println("string to byte array : " 
              + Arrays.toString(byteswithcharset1));

    // convert string to byte array java with charset defined
    byte[] byteswithcharset2 = string.getBytes(Charset.forName("ISO-8859-1"));
    System.out.println("string to byte array : " 
              + Arrays.toString(byteswithcharset2));

    byte[] bytesUsingBase64 = Base64.getDecoder().decode(string);
    System.out.println("string to byte array using Base64 : "
              + Arrays.toString(bytesUsingBase64));
   }
 }

Output:

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

Conclusion:

In this tutorial, we have covered how to convert String to Byte Array in Java using 4 ways
  1. Using getBytes()
  2. Using getBytes(String charsetName) throws UnsupportedEncodingException
  3. Using getBytes(Charset charset)
  4. Using Base64.getDecoder().decode(String 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