Unicode转成中文
public String convert(String utfString){
StringBuilder sb = new StringBuilder();
int i = -1;
int pos = 0;
while((i=utfString.indexOf("\\u", pos)) !=-1){
sb.append(utfString.substring(pos,i));
if(i+5 < utfString.length()){
pos = i+6;
sb.append((char)Integer.parseInt(utfString.substring(i+2,i+6), 16));
}
}
return sb.toString();
}
中文转成unicode
1. /**
2. * 把中文转成Unicode码
3. * @param str
4. * @return
5. */
6. public String chinaToUnicode(String str){
7. String result="";
8. for (int i = 0; i < str.length(); i++){
9. int chr1 = (char) str.charAt(i);
10. if(chr1>=19968&&chr1<=171941){//汉字范围 \u4e00-\u9fa5 (中文)
11. result+="\\u" + Integer.toHexString(chr1);
12. }else{
13. result+=str.charAt(i);
14. }
15. }
16. return result;
17. }
18.
19. /**
20. * 判断是否为中文字符
21. * @param c
22. * @return
23. */
24. public boolean isChinese(char c) {
25. Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
26. if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
27. || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
28. || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
29. || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
30. || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
31. || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
32. return true;
33. }
34. return false;
35. }