如何用java将unicode字符串转换为ASCII码

时间:2022-04-11 15:54:21

I'm now trying to convert unicode font to ascii in android. I wrote following coding to convert unicode font to ascii but it's failed. Because result cannot display properly after being converted.

我现在正尝试在android中把unicode字体转换成ascii码。我编写了下面的代码将unicode字体转换为ascii码,但是失败了。因为结果在转换后不能正常显示。

unicode font = 'ေနေကာင္းပါသလား' to something like '\u100F\u1039\u100D'

unicode字体= 'ေနေကာင္းပါသလား’,‘\ 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 个解决方案

#1


6  

use java.text.Normalizer class to convert from unicode to ascii. here is a sample code from the answer https://*.com/a/2097224/931982

使用text。将unicode转换为ascii的规范化类。下面是来自于https://*.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());

#1


6  

use java.text.Normalizer class to convert from unicode to ascii. here is a sample code from the answer https://*.com/a/2097224/931982

使用text。将unicode转换为ascii的规范化类。下面是来自于https://*.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());