我能够弄清楚如何使用以下代码将 Unicode 字符串转换为 ASCII 字符串。(学分在代码中)
//create a string using unicode that says "hello" when printed to console
String unicode = "\u0068" + "\u0065" + "\u006c" + "\u006c" + "\u006f";
System.out.println(unicode);
System.out.println("");
/* Test code for converting unicode to ASCII
* Taken from http://stackoverflow.com/questions/15356716/how-can-i-convert-unicode-string-to-ascii-in-java
* Will be commented out later after tested and implemented.
*/
//String s = "口水雞 hello Ä";
//replace String s with String unicode for conversion
String s1 = Normalizer.normalize(unicode, Normalizer.Form.NFKD);
String regex = Pattern.quote("[\\p{InCombiningDiacriticalMarks}\\p{IsLm}\\p{IsSk}]+");
String s2 = new String(s1.replaceAll(regex, "").getBytes("ascii"), "ascii");
System.out.println(s2);
System.out.println(unicode.length() == s2.length());
//End of Test code that was implemented
现在,我的问题和好奇心已经战胜了我。我尝试用谷歌搜索,因为我对 Java 没有最好的了解。
我的问题是,是否可以将 ASCII 字符串转换为 UTF 格式?特别是 UTF-16。(我说 UTF-16 是因为我知道 UTF-8 与 ASCII 有多么相似,并且没有必要从 ASCII 转换为 UTF-8)
提前致谢!