Convert String to UTF-8 bytes in Java

Java 8Object Oriented ProgrammingProgramming

Before converting a String to UTF-8-bytes, let us have a look at UTF-8.

UTF-8 is a variable width character encoding. UTF-8 has ability to be as condense as ASCII but can also contain any unicode characters with some increase in the size of the file. UTF stands for Unicode Transformation Format. The '8' signifies that it allocates 8-bit blocks to denote a character. The number of blocks needed to represent a character varies from 1 to 4.

In order to convert a String into UTF-8, we use the getBytes() method in Java. The getBytes() method encodes a String into a sequence of bytes and returns a byte array.

Declaration − The getBytes() method is declared as follows −

public byte[] getBytes(String charsetName)

where charsetName is the specific charset by which the String is encoded into an array of bytes.

Let us see a program to convert a String into UTF-8 bytes in Java.

Example

 Live Demo

public class Example {
   public static void main(String args[]) throws Exception {
      String s = "Hello World";
      byte arr[] = s.getBytes("UTF8");
      for (byte x: arr) {
         System.out.print(x+" ");
      }
   }
}

Output

72 101 108 108 111 32 87 111 114 108 100

Let us understand the above program. We have created a String s −

String s = "Hello World";

String s is assigned the value Hello World.

To convert it into UTF-8, we use the getBytes(“UTF-8”) method. This gives us an array of bytes as follows −

byte[] arr = s.getBytes("UTF-8");

Then to print the byte array, we use an enhanced for loop as follows −

for (byte x: arr) {
   System.out.print(x+" ");
}
raja
Published on 04-Jan-2019 10:34:41
Advertisements