如何在java中将unicode字符串转换为ASCII

时间:2013-03-12 08:46:23

标签: java android unicode ascii

我现在正在尝试将unicode字体转换为android中的ascii。我编写了以下编码来将unicode字体转换为ascii,但它失败了。因为转换后结果无法正常显示。

unicode font ='ေနေကာင္း''类似'\ u100F \ u1039 \ u100D'

public static String toJAVA (String zawgyi) {
    String output = "";
    char[] charArray = zawgyi.toCharArray();

    for (int i = 0; i < charArray.length; i++) {
        char a = charArray[i];
        if ((int) a > 255) {
            output += "\\u" + Integer.toHexString((int) a) + "--";
        } else {
            output += a;
        }
    }       
    return output;
}

1 个答案:

答案 0 :(得分:7)

使用java.text.Normalizer类将unicode转换为ascii。这是答案https://stackoverflow.com/a/2097224/931982

的示例代码
String s = "口水雞 hello Ä";

String s1 = Normalizer.normalize(s, 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(s.length() == s2.length());