2
13693261870
2022-09-16 653761a31dfeb50dd3d007e892d69c90bf0cdafc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.landtool.lanbase.common.utils;
 
public class CommonUtils {
    public static String subStringUtils(String str, int end) {
        int length = 0;
        char[] ch = str.toCharArray();
        String str2 = "";
        for (int i = 0; i < str.length(); i++) {
            int num = str.codePointAt(i);
            if (num >= 0 && num <= 255) {
                length++;
            } else {
                length += 2;
            }
            if (length < end) {
                str2 += ch[i];
            } else if (length == end) {
                str2 += ch[i];
                if (i < str.length() - 1) {
                    str2 += "...";
                }
                break;
            } else {
                break;
            }
 
        }
        return str2;
    }
 
    /**
     * 中文转Unicode
     * @param gbString 转换中文字符串
     * @return 转换后Unicode
     */
    public static String gbEncoding(final String gbString) {
        char[] utfBytes = gbString.toCharArray();
        String unicodeBytes = "";
        for(int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++) {
            String hexB = Integer.toHexString(utfBytes[byteIndex]);//转换为16进制整型字符串
            if(hexB.length() <= 2) {
                hexB = "00" + hexB;
            }
            unicodeBytes = unicodeBytes + "\\u" + hexB;
        }
 
        return unicodeBytes;
    }
 
    /**
     * Unicode转中文
     * @param dataStr Unicode字符串
     * @return 转换后中文
     */
    public static String decodeUnicode(final String dataStr) {
        int start = 0;
        int end = 0;
        final StringBuffer buffer = new StringBuffer();
        while (start > -1){
            end = dataStr.indexOf("\\u", start + 2);
            String charStr = "";
            if(end == -1) {
                charStr = dataStr.substring(start +2, dataStr.length());
            } else {
                charStr = dataStr.substring(start + 2, end);
            }
            char letter = (char) Integer.parseInt(charStr, 16);
            buffer.append(new Character(letter).toString());
            start = end;
        }
 
        return buffer.toString();
    }
}