10

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.

unicode font = 'ေနေကာင္းပါသလား' to something like '\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;
}
2

1 Answer 1

8

use java.text.Normalizer class to convert from unicode to ascii. here is a sample code from the answer 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());
3
  • Sorry, got following error.12 15:41:10.909: E/AndroidRuntime(25891): java.util.regex.PatternSyntaxException: U_ILLEGAL_ARGUMENT_ERROR 03-12 15:41:10.909: E/AndroidRuntime(25891): [\p{InCombiningDiacriticalMarks}\p{IsLm}\p{IsSk}]+
    – PPShein
    Mar 12, 2013 at 9:13
  • now check i have added the line String regex = Pattern.quote("[\\p{InCombiningDiacriticalMarks}\\p{IsLm}\\p{IsSk}]+"); ... I just checked by myself
    – stinepike
    Mar 12, 2013 at 9:31
  • where did you add above line to?
    – PPShein
    Mar 12, 2013 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.