Java Data Type How to - Create unicode from string "\u00C3" etc








Question

We would like to know how to create unicode from string "\u00C3" etc.

Answer

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.regex.Pattern;
/*  ww w  . j  a  v a 2 s  .  c om*/
public class Main {

  private static final Pattern UCODE_PATTERN = Pattern
      .compile("\\\\u[0-9a-fA-F]{4}");

  public static void main(String[] args) throws Exception {
    BufferedReader br = new BufferedReader(new FileReader("ravi.txt"));
    while (true) {
      String line = br.readLine();
      if (line == null)
        break;
      if (!UCODE_PATTERN.matcher(line).matches()) {
        System.err.println("Bad input: " + line);
      } else {
        String hex = line.substring(2, 6);
        int number = Integer.parseInt(hex, 16);
        System.out.println(hex + " -> " + ((char) number));
      }
    }
  }
}