Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I convert unicode string to ASCII in java

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;
}
like image 554
PPShein Avatar asked Mar 12 '13 08:03

PPShein


Video Answer


1 Answers

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());
like image 50
stinepike Avatar answered Oct 21 '22 09:10

stinepike