Convert International String to \u Codes in java

后端 未结 12 2044
离开以前
离开以前 2020-11-29 02:53

How can I convert an international (e.g. Russian) String to \\u numbers (unicode numbers)
e.g. \\u041e\\u041a for OK ?

12条回答
  •  日久生厌
    2020-11-29 03:10

    there is a JDK tools executed via command line as following :

    native2ascii -encoding utf8 src.txt output.txt
    

    Example :

    src.txt

    بسم الله الرحمن الرحيم
    

    output.txt

    \u0628\u0633\u0645 \u0627\u0644\u0644\u0647 \u0627\u0644\u0631\u062d\u0645\u0646 \u0627\u0644\u0631\u062d\u064a\u0645
    

    If you want to use it in your Java application, you can wrap this command line by :

    String pathSrc = "./tmp/src.txt";
    String pathOut = "./tmp/output.txt";
    String cmdLine = "native2ascii -encoding utf8 " + new File(pathSrc).getAbsolutePath() + " " + new File(pathOut).getAbsolutePath();
    Runtime.getRuntime().exec(cmdLine);
    System.out.println("THE END");
    

    Then read content of the new file.

提交回复
热议问题