1
2
3
4
5 package gboat2.base.bridge.util;
6
7
8
9
10
11
12
13
14
15 public final class CharUtil {
16
17
18
19
20
21
22
23
24
25 public static String chinaToUnicode(String str, boolean isWithU) {
26 String result = "";
27 for (int i = 0; i < str.length(); i++) {
28 int chr1 = (char) str.charAt(i);
29 if (chr1 >= 19968 && chr1 <= 171941) {
30 if (isWithU) {
31 result += "\\u" + Integer.toHexString(chr1);
32 } else {
33 result += Integer.toHexString(chr1);
34 }
35 } else {
36 result += str.charAt(i);
37 }
38 }
39 return result;
40 }
41
42
43
44
45
46
47
48
49
50 public static String unicode2China(String str, boolean withU) {
51 StringBuffer out = new StringBuffer("");
52 if (!withU) {
53 for (int i = 0; i < str.length(); i += 4) {
54 out.append("\\u");
55 out.append(str.substring(i, i + 4));
56 }
57 } else {
58 out.append(str);
59 }
60
61
62 String s[] = out.toString().split("\\\\u");
63 String t = "";
64 for (int j = 1; j < s.length; j++) {
65 int ab = Integer.valueOf(s[j], 16);
66 t = t + (char) ab;
67 }
68 return t;
69 }
70 }