“mitao”
2026-04-07 3ee4725e50f2494fc291ba841730942004e22956
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
package com.xiaopeng.trafficControl.util;
 
import com.xiaopeng.trafficControl.DecodingException;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
 
 
/**
 * 与字节16进制相关的工具类<br/>
 */
public class HexUtil {
    /** 将十六进制字符串转成字节数组 */
    public static byte[] toHexBytes(String hexText) {
        try {
            return Hex.decodeHex(hexText.toCharArray());
        } catch (DecoderException e) {
            throw new DecodingException(e);
        }
    }
 
    /** 将字节数组转成十六进制字符串(字母小写) */
    public static String toLowerCase(byte[] hex) {
        return new String(Hex.encodeHex(hex, true));
    }
 
    /** 将字节数组转成十六进制字符串(字母大写) */
    public static String toUpperCase(byte[] hex) {
        return new String(Hex.encodeHex(hex, false));
    }
 
    /** 将字节数组转成整数 */
    public static int hashToInt(byte[] hash) {
        return (int) (hash[3] << 24 | hash[2] << 16 | hash[1] << 8 | hash[0]);
    }
 
}