826220679@qq.com
9 天以前 6d5ff381cafca9b82e11407dc67bf6b74f1397ee
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
package dell55AAData;
 
public class HexUtils {
    // Ê®Áù½øÖÆ×Ö·û¿ìËÙת»»±í (ASCII·¶Î§ÄÚ)
    private static final int[] HEX_VALUES = new int[128];
    static {
        for (int i = 0; i < HEX_VALUES.length; i++) {
            char c = (char) i;
            if (c >= '0' && c <= '9') HEX_VALUES[i] = c - '0';
            else if (c >= 'A' && c <= 'F') HEX_VALUES[i] = 10 + (c - 'A');
            else if (c >= 'a' && c <= 'f') HEX_VALUES[i] = 10 + (c - 'a');
            else HEX_VALUES[i] = -1;
        }
    }
 
    // Ḭ̈߳²È«µÄ×Ö·û»º³åÇø (³õʼ´óС256)
    private static final ThreadLocal<char[]> CHAR_BUF_CACHE = 
        ThreadLocal.withInitial(() -> new char[256]);
 
    /**
     * »ñÈ¡Ï̱߳¾µØ×Ö·û»º³åÇø
     * @return ¿É¸´ÓõÄchar[256]»º³åÇø
     */
    public static char[] getThreadLocalBuffer() {
        return CHAR_BUF_CACHE.get();
    }
 
    /**
     * ¿ìËÙ½«Á½¸öÊ®Áù½øÖÆ×Ö·ûת»»Îª×Ö½Ú
     * @param c1 ¸ßλ×Ö·û (0-9, A-F, a-f)
     * @param c2 µÍλ×Ö·û (0-9, A-F, a-f)
     * @return ×ª»»ºóµÄ×Ö½ÚÖµ (ÎÞЧ×Ö·û·µ»Ø0)
     */
    public static int fastHexToByte(char c1, char c2) {
        int high = (c1 < 128) ? HEX_VALUES[c1] : -1;
        int low = (c2 < 128) ? HEX_VALUES[c2] : -1;
        if (high < 0 || low < 0) return 0;
        return (high << 4) | low;
    }
}