ByteUtil.java 5.3 KB
package com.ruoyi.common.utils;

import java.util.Arrays;

public class ByteUtil {
    /**
     * byte数组中取int数值,本方法适用于(低位在前,高位在后)的顺序,和和intToBytes()配套使用
     *
     * @param src
     *            byte数组
     * @param offset
     *            从数组的第offset位开始
     * @return int数值
     */
    public static long bytesToLongASC(byte[] src, int offset,int lenth) {
        int value = 0;
        for(int i=0;i<lenth;i++)
        {
            value = value | ((src[offset+i] & 0xFF)<<(8*i));
        }
        return value;
    }

    /**
     * 把16进制字符串转换成字节数组
     *
     * @param hex
     * @return
     */
    public static byte[] hexStringToByte(String hex) {
        int len = (hex.length() / 2);
        byte[] result = new byte[len];
        char[] achar = hex.toCharArray();
        for (int i = 0; i < len; i++) {
            int pos = i * 2;
            result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
        }
        return result;
    }
    private static byte toByte(char c) {
        byte b = (byte) "0123456789ABCDEF".indexOf(c);
        return b;
    }

    /**
     * 把16进制字符串转换成字节数组
     *
     * @param hex
     * @return
     */
    public static String hexStringToSpace(String hex) {
        if (null == hex) {
            return null;
        } else {
            StringBuilder sb = new StringBuilder(hex.length() << 1);

            for(int i = 0; i < hex.length(); i+=2) {
                sb.append(hex.substring(i,i+2)).append(" ");
            }
            return sb.toString();
        }
    }

    /**
     * 把原数组加点目标数组后面
     * @param dest 目标数组
     * @param src 原数组
     * @return
     */
    public static byte[] addBytes(byte[] dest,byte[] src )
    {
        int dl = dest.length;
        int sl = src.length;
        dest = Arrays.copyOf(dest, dl+sl);//数组扩容
        System.arraycopy(src,0,dest,dl,src.length);
        return dest;
    }

    /**
     * 将int数值转换为占四个字节的byte数组,本方法适用于(高位在前,低位在后)的顺序。  和bytesToInt2()配套使用
     */
    public static byte[] intToBytesDESC(long value,int lenth)
    {
        byte[] src = new byte[lenth];
        for(int i=0;i<lenth;i++)
        {
            src[i] = (byte) ((value>>(8*(lenth-i-1))) & 0xFF);
        }
        return src;
    }

    /**
     * 将int数值转换为占四个字节的byte数组,本方法适用于(低位在前,高位在后)的顺序。 和bytesToInt()配套使用
     * @param value
     *            要转换的int值
     * @return byte数组
     */
    public static byte[] intToBytesASC( long value,int lenth)
    {
        byte[] src = new byte[lenth];
        for(int i=lenth;i>0;i--)
        {
            src[i-1] =  (byte) ((value>>(8*(i-1))) & 0xFF);
        }
        return src;
    }

    public static void main(String[] args) {
        System.out.println(ByteUtil.toHexString( ByteUtil.intToBytesASC(2011239256,4)));
    }

    /**
     * ip转化位4byte
     * @param ip
     * @return
     */
    public static byte[] ipTo4Byte(String ip)
    {
        String[] ips = ip.split(".");
        return new byte[]{(byte) Integer.parseInt(ips[0]),(byte) Integer.parseInt(ips[1]),(byte) Integer.parseInt(ips[2]),(byte) Integer.parseInt(ips[3])};
    }

    /**
     * byte数组中取int数值,本方法适用于(低位在后,高位在前)的顺序。和intToBytes2()配套使用
     */
    public static long bytesToLongDESC(byte[] src, int offset,int lenth) {
        long value = 0;
        for(int i=lenth;i>0;i--)
        {
            value = value | ((src[offset+(lenth-i)] & 0xFF)<<(8*(i-1)));
        }
        return value;
    }

    private static final char[] hex = "0123456789abcdef".toCharArray();
    public static String toHexString(byte[] bytes) {
        if (null == bytes) {
            return null;
        } else {
            StringBuilder sb = new StringBuilder(bytes.length << 1);

            for(int i = 0; i < bytes.length; ++i) {
                sb.append(hex[(bytes[i] & 240) >> 4]).append(hex[bytes[i] & 15]);
            }

            return sb.toString();
        }
    }

    /**
     * 计算CRC16/Modbus校验码  低位在前,高位在后
     *
     * @param str 十六进制字符串
     * @return
     */
    public static String getCRC16(String str) {
        byte[] bytes = hexStringToByte(str);
        return getCRC16(bytes);
    }

    /**
     * 计算CRC16/Modbus校验码  低位在前,高位在后
     *
     * @return
     */
    public static String getCRC16(  byte[] bytes) {
        int CRC = 0x0000ffff;
        int POLYNOMIAL = 0x0000a001;

        int i, j;
        for (i = 0; i < bytes.length; i++) {
            CRC ^= ((int) bytes[i] & 0x000000ff);
            for (j = 0; j < 8; j++) {
                if ((CRC & 0x00000001) != 0) {
                    CRC >>= 1;
                    CRC ^= POLYNOMIAL;
                } else {
                    CRC >>= 1;
                }
            }
        }
        String crc = Integer.toHexString(CRC);
        if (crc.length() == 2) {
            crc = "00" + crc;
        } else if (crc.length() == 3) {
            crc = "0" + crc;
        }
        crc = crc.substring(2, 4) + crc.substring(0, 2);
        return crc.toUpperCase();
    }
}