我怎么可以转换UNI code字符串在Java中为ASCII [英] how can I convert unicode string to ASCII in java
本文介绍了我怎么可以转换UNI code字符串在Java中为ASCII的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我现在想转换UNI code字体在Android中使用ASCII。我写了下面的编码转换UNI code字体为ascii,但它的失败。因为结果不能被转换后的正常显示。
UNI code字型='ေနေကာင္းပါသလား喜欢的东西'\ u100F \ u1039 \ u100D'
公共静态字符串toJAVA(字符串zawgyi){
字符串输出=;
的char [] charArray = zawgyi.toCharArray();
的for(int i = 0; I< charArray.length;我++){
所以char a = charArray [I]
如果((int)的a取代; 255){
输出+ =\\ U+ Integer.toHexString((INT)一)+ - ;
} 其他 {
输出+ =一个;
}
}
返回输出;
}
解决方案
使用java.text.Normalizer类从单向code转换为ASCII。这里距离答案样本code http://stackoverflow.com/a/2097224/931982
的String =口水鸡喂了;
字符串S1 = Normalizer.normalize(S,Normalizer.Form.NFKD);
字符串的正则表达式= Pattern.quote([\\ p {InCombiningDiacriticalMarks} \\ p {} ISLM \\ p {IsSk}] +);
字符串s2 =新的String(s1.replaceAll(正则表达式,).getBytes(ASCII),ASCII);
的System.out.println(S2);
的System.out.println(s.length()== s2.length());
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;
}
解决方案
use java.text.Normalizer class to convert from unicode to ascii. here is a sample code from the answer http://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());
这篇关于我怎么可以转换UNI code字符串在Java中为ASCII的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文