Java String getBytes() Method

Java String getBytes() | The getBytes() method of the Java String class encodes the string into the sequence of bytes and stores the same into the byte array. This is a sequence of bytes using the named charset where the result is stored in a byte array. The getBytes() method has three variations as follows:-

  1. public byte[ ] getBytes(String charsetName) throws UnsupportedEncodingException
  2. public byte[ ] getBytes(Charset charset)
  3. public byte[ ] getBytes()

The (1) & (3) methods were introduced in Java 1.1 whereas the (2) was introduced in the Java1.6 version. All of these variations of getBytes() return an array of byte values.

Java String getBytes Default Encoding

The public byte[ ] getBytes() encodes this String into a sequence of bytes using the platform’s default charset, storing the result into a new byte array. The behavior of this method when this string cannot be encoded in the default charset is unspecified. The java.nio.charset.CharsetEncoder class should be used when more control over the encoding process is required.

import java.util.Arrays;

public class Main {
   public static void main(String args[]) {
      String str = "Hello World!";
      byte[] byteArray = str.getBytes();
      System.out.println(Arrays.toString(byteArray));
   }
}

Output:-

[72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]

By default, it is returning the array of equivalent ASCII values for the characters. The ASCII value of ‘a’ to ‘z’ is 97 to 122, ‘A’ to ‘Z’ is 65 to 95, and ‘0’ to ‘9’ is 48 to 57. See more here:- Java Program to Find ASCII Value.

Java String getBytes Encoding

We can also pass different encoding type names as strings to get a byte array according to the given encoding type. For this public byte[ ] getBytes(String charsetName) throws UnsupportedEncodingException method will be called. If the named charset is not supported then this getBytes() method throws UnsupportedEncodingException. Let us see it through an example.

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

public class Main {
   public static void main(String argvs[]) {
      String string = "Welcome to KnowProgram.";
      System.out.println("Input String: " + string);
      try {
         byte[] arrayUTF16 = string.getBytes("UTF-16");
         System.out.println("In UTF-16: " 
              + Arrays.toString(arrayUTF16));

         byte[] arrayUTF16BE = string.getBytes("UTF-16BE");
         System.out.println("In UTF-16BE: " 
              + Arrays.toString(arrayUTF16BE));

         byte[] arrayISO8859 = string.getBytes("ISO-8859-1");
         System.out.println("In ISO-8859-1: " 
              + Arrays.toString(arrayISO8859));

         byte[] arrayUTF16LE = string.getBytes("UTF-16LE");
         System.out.println("In UTF-16LE : " 
              + Arrays.toString(arrayUTF16LE));
      } catch (UnsupportedEncodingException uee) {
         System.out.println("Unsupported character set" + uee);
      }
   }
}

Output:-

Input String: Welcome to KnowProgram.
In UTF-16: [-2, -1, 0, 87, 0, 101, 0, 108, 0, 99, 0, 111, 0, 109, 0, 101, 0, 32, 0, 116, 0, 111, 0, 32, 0, 75, 0, 110, 0, 111, 0, 119, 0, 80, 0, 114, 0, 111, 0, 103, 0, 114, 0, 97, 0, 109, 0, 46]
In UTF-16BE: [0, 87, 0, 101, 0, 108, 0, 99, 0, 111, 0, 109, 0, 101, 0, 32, 0, 116, 0, 111, 0, 32, 0, 75, 0, 110, 0, 111, 0, 119, 0, 80, 0, 114, 0, 111, 0, 103, 0, 114, 0, 97, 0, 109, 0, 46]
In ISO-8859-1: [87, 101, 108, 99, 111, 109, 101, 32, 116, 111, 32, 75, 110, 111, 119, 80, 114, 111, 103, 114, 97, 109, 46]
In UTF-16LE : [87, 0, 101, 0, 108, 0, 99, 0, 111, 0, 109, 0, 101, 0, 32, 0, 116, 0, 111, 0, 32, 0, 75, 0, 110, 0, 111, 0, 119, 0, 80, 0, 114, 0, 111, 0, 103, 0, 114, 0, 97, 0, 109, 0, 46, 0]

Java String getBytes() Charset

We can also pass charset in the getBytes() method. For this public byte[ ] getBytes(Charset charset) method will be called. The StandardCharsets class contains the following charsets:-

  • US_ASCII
  • ISO_8859_1
  • UTF_8
  • UTF_16BE
  • UTF_16LE
  • UTF_16

Let us see an example of the getBytes(Charset charset) method by passing the StandardCharsets class field. Unlike passing charsetName, when we directly pass charset then the getBytes() method won’t throw any exception.

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class Main {
   public static void main(String argvs[]) {
      String string = "Hello, World!";
      System.out.println("Input String: " + string);

      byte[] arrayUTF8 = string.getBytes(StandardCharsets.UTF_8);
      System.out.println("Byte array in UTF-8: " 
               + Arrays.toString(arrayUTF8));

      byte[] arrayUTF16 = string.getBytes(StandardCharsets.UTF_16);
      System.out.println("Byte array in UTF-16: " 
               + Arrays.toString(arrayUTF16));

      byte[] arrayUTF16BE = string.getBytes(StandardCharsets.UTF_16BE);
      System.out.println("Byte array in UTF-16BE: " 
               + Arrays.toString(arrayUTF16BE));

      byte[] arrayUTF16LE = string.getBytes(StandardCharsets.UTF_16LE);
      System.out.println("Byte array in UTF-16LE: " 
               + Arrays.toString(arrayUTF16LE));
   }
}

Output:-

Input String: Hello, World!
Byte array in UTF-8: [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]
Byte array in UTF-16: [-2, -1, 0, 72, 0, 101, 0, 108, 0, 108, 0, 111, 0, 44, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100, 0, 33]
Byte array in UTF-16BE: [0, 72, 0, 101, 0, 108, 0, 108, 0, 111, 0, 44, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100, 0, 33]
Byte array in UTF-16LE: [72, 0, 101, 0, 108, 0, 108, 0, 111, 0, 44, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100, 0, 33, 0]

Get notes to make your learning process easy. These are specially designed for beginners who want to learn coding through simple words, programs, and examples. You can use it as your reference and for revision purposes.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.