kongdeqiang
2023-07-10 4f27e2a21aa7c0cbd07447b43fc3b83fd1525f88
src/main/java/com/boying/util/StringUtil.java
@@ -5,6 +5,12 @@
public class StringUtil {
    /** 16进制中的字符集 */
    private static final String HEX_CHAR = "0123456789ABCDEF";
    /** 16进制中的字符集对应的字节数组 */
    private static final byte[] HEX_STRING_BYTE = HEX_CHAR.getBytes();
    public static boolean isNullOrEmpty(String str) {
        if (null == str || "".equalsIgnoreCase(str.trim())
                || "null".equals(str.trim())) {
@@ -31,6 +37,27 @@
        return s2;
    }
    public static byte[] toBytes(String strs){
        String[] split = strs.split("-");
        byte[] bytes = new byte[split.length];
        for (int i = 0; i < split.length; i++) {
            String s = split[i];
            bytes[i] = (byte) Byte.parseByte(s);
        }
        return bytes;
    }
    public static byte[] hex2Byte(String hex) {
        System.out.println();
        String[] parts = hex.split("-");
        byte[] bytes = new byte[parts.length];
        for (int i = 0; i < parts.length; i++) {
            bytes[i] = (byte) (Integer.parseInt(parts[i],10));
            System.out.print(bytes[i]+ " ");
        }
        return bytes;
    }
    public static String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }
@@ -51,7 +78,6 @@
            }
            System.out.print(hex.toUpperCase() + " ");
        }
        System.out.println("");
    }
    /**
@@ -81,11 +107,11 @@
        if (null == src || 0 == src.length()) {
            return null;
        }
        String s1 = src.replaceAll("-", "");
        byte[] ret = new byte[s1.length() / 2];
        byte[] tmp = s1.getBytes();
       // String s1 = src.replaceAll("-", "");
        byte[] ret = new byte[src.length() / 2];
        byte[] tmp = src.getBytes();
        for (int i = 0; i < (tmp.length / 2); i++) {
            ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
            ret[i] = (byte)uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
        }
        return ret;
    }
@@ -105,12 +131,70 @@
        return ret;
    }
    public static void main(String[] args) {
        String s = "00-64-FF-FF-6E-78-00-04-00-01-01-02-00-FF-00-00-00-09-E4-BA-AC-41-38-38-38-38-38-0D-01-01-01-02-00-FF-00-00-00-09-E4-B8-B4-E6-97-B6-E8-BD-A6-0D-02-01-01-02-00-FF-00-00-00-0C-E6-AC-A2-E8-BF-8E-E5-85-89-E4-B8-B4-0D-03-01-01-02-00-FF-00-00-00-09-E4-BD-99-E4-BD-8D-3A-37-34-00-0A-20-E4-B8-B4-E6-97-B6-E8-BD-A6-2C-E4-BA-AC-41-38-38-38-38-38-2C-E6-AC-A2-E8-BF-8E-E5-85-89-E4-B8-B4-00-6B-E7";
        String s1 = s.replaceAll("-", "");
        byte[] bytes = HexString2Bytes(s1);
        for (byte aByte : bytes) {
            System.out.print(aByte + " ");
    /**
     * 10进制字节数组转换为16进制字节数组
     *
     * byte用二进制表示占用8位,16进制的每个字符需要用4位二进制位来表示,则可以把每个byte
     * 转换成两个相应的16进制字符,即把byte的高4位和低4位分别转换成相应的16进制字符,再取对应16进制字符的字节
     *
     * @param b 10进制字节数组
     * @return 16进制字节数组
     */
    public static byte[] byte2hex(byte[] b) {
        int length = b.length;
        byte[] b2 = new byte[length << 1];
        int pos;
        for(int i=0; i<length; i++) {
            pos = 2*i;
            b2[pos] = HEX_STRING_BYTE[(b[i] & 0xf0) >> 4];
            b2[pos+1] = HEX_STRING_BYTE[b[i] & 0x0f];
        }
        return b2;
    }
    /**
     * 16进制字节数组转换为10进制字节数组
     *
     * 两个16进制字节对应一个10进制字节,则将第一个16进制字节对应成16进制字符表中的位置(0~15)并向左移动4位,
     * 再与第二个16进制字节对应成16进制字符表中的位置(0~15)进行或运算,则得到对应的10进制字节
     * @param b 10进制字节数组
     * @return 16进制字节数组
     */
    public static byte[] hex2byte(byte[] b) {
        if(b.length%2 != 0) {
            throw new IllegalArgumentException("byte array length is not even!");
        }
        int length = b.length >> 1;
        byte[] b2 = new byte[length];
        int pos;
        for(int i=0; i<length; i++) {
            pos = i << 1;
            b2[i] = (byte) (HEX_CHAR.indexOf( b[pos] ) << 4 | HEX_CHAR.indexOf( b[pos+1] ) );
        }
        return b2;
    }
    /**
     * 将16进制字节数组转成10进制字符串
     * @param b 16进制字节数组
     * @return 10进制字符串
     */
    public static String hex2Str(byte[] b) {
        return new String(hex2byte(b));
    }
    /**
     * 将10进制字节数组转成16进制字符串
     * @param b 10进制字节数组
     * @return 16进制字符串
     */
    public static String byte2HexStr(byte[] b) {
        return Integer.toHexString(Integer.parseInt(new String(b)));
    }
    public static void main(String[] args) {
    }
}