Convert International String to \u Codes in java

后端 未结 12 2051
离开以前
离开以前 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:16

    There is an Open Source java library MgntUtils that has a Utility that converts Strings to unicode sequence and vise versa:

    result = "Hello World";
    result = StringUnicodeEncoderDecoder.encodeStringToUnicodeSequence(result);
    System.out.println(result);
    result = StringUnicodeEncoderDecoder.decodeUnicodeSequenceToString(result);
    System.out.println(result);
    

    The output of this code is:

    \u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064
    Hello World
    

    The library can be found at Maven Central or at Github It comes as maven artifact and with sources and javadoc

    Here is javadoc for the class StringUnicodeEncoderDecoder

提交回复
热议问题