Saturday 8 February 2020

Convert UTF-16 unicode characters to UTF-8 in java

Step 1: Get UTF_8 byte array from utf16 unicode string
String utf16EncodedChars = "\u0048 \u0065 \u006c \u006c \u006f \u0020 \u0057 \u006f \u0072 \u006c \u0064";
byte[] utf8Bytes = utf16EncodedChars.getBytes(StandardCharsets.UTF_8);

Step 2: Get the string object from byte array.
String result = new String(utf8Bytes, StandardCharsets.UTF_8);

App.java
package com.sample.app;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

public class App {

 public static void main(String args[]) throws UnsupportedEncodingException {
  String utf16EncodedChars = "\u0048 \u0065 \u006c \u006c \u006f \u0020 \u0057 \u006f \u0072 \u006c \u0064";

  byte[] utf8Bytes = utf16EncodedChars.getBytes(StandardCharsets.UTF_8);

  String result = new String(utf8Bytes, StandardCharsets.UTF_8);

  System.out.println(result);
 }

}

Output
H e l l o   W o r l d


You may like

No comments:

Post a Comment