A byte processing entity class used in java iot development

When developing and interacting with hardware, it is often necessary to convert byte data into basic types:

1. Splicing of byte data

2. Merging of byte data

3. Interception of byte data

4. Convert bytes to basic type data, and convert basic type data to byte array…

public class ByteArrayUtil {

    /**
     * byte array splicing
     * @param first
     * @param back
     * @return
     */
    public static byte[] append(byte[] first, byte[] back) {
        if(null == first || null == back){
            return null;
        }
        int length = first.length + back.length;
        byte[] res = new byte[length];
        System.arraycopy(first, 0, res, 0, first.length);
        System.arraycopy(back, 0, res, first.length, back.length);
        return res;

    }

    public static byte[] byteMergerAll(byte[]... args) {
        int length_byte = 0;
        for (byte[] b : args) {
            length_byte + = b.length;
        }
        byte[] all_byte = new byte[length_byte];
        int countLength = 0;
        for (byte[] b : args) {
            System.arraycopy(b, 0, all_byte, countLength, b.length);
            countLength + = b.length;
        }
        return all_byte;
    }

    /**
     *subbyte
     * @param data
     * @param off Number of bit shifts = physical displacement N bits, the next bit is the real data
     * @param length
     * @return
     */
    public static byte[] subBytes(byte[] data,int off,int length){
        byte[] res = new byte[length];
        System.arraycopy(data, off, res, 0, length);
        return res;
    }


    /**
     * short converted to byte[]
     * @param number
     * @return byte[]
     */
    public static byte[] short2Bytes(short number) {
        byte[] b = new byte[2];
        b[0] = (byte) (number >> 8);
        b[1] = (byte) (number & amp; 0xFF);
        return b;
    }

    /**
     * byte[] converted to short
     * @param bytes
     * @return short
     */
    public static short bytes2Short(byte[] bytes){
        short l = 0;
        for (int i = 0; i < 2; i + + ) {
            l<<=8; //<<= is the same as our + =, which means l = l << 8
            l |= (bytes[i] & amp; 0xff); //Same as above l = l | (b[i] & amp;0xff)
        }
        return l;
    }

    /**
     * byte to int
     *
     * @param data
     * @return
     */
    public static int bytes2int(byte[] data) {
        int mask = 0xff;
        int temp = 0;
        int n = 0;
        for (int i = 0; i < data.length; i + + ) {
            n <<= 8;
            temp = data[i] & amp; mask;
            n |= temp;
        }
        return n;
    }


    /**
     * int to bytes
     *
     * @param n
     * @return
     */
    public static byte[] int2bytes(int n) {
        byte[] b = new byte[4];

        for (int i = 0; i < 4; i + + ) {
            b[i] = (byte) (n >> (24 - i * 8));

        }
        return b;
    }

    /**
     * byte[] converted to GBK encoded string
     * @param byteArray
     * @return
     */
    public static String bytes2gbkString(byte[] byteArray) {
        return bytes2string(byteArray,"GBK");
    }

    /**
     * byte[] converted to string
     * @param byteArray
     * @param charset
     * @return
     */
    public static String bytes2string(byte[] byteArray, String charset) {
        if (byteArray.length == 0) {
            return "";
        }
        return new String(byteArray, Charset.forName(charset));
    }

    /**
     * byte[] converted to string
     * @param byteArray
     * @return
     */
    public static String bytes2string(byte[] byteArray) {
        if (byteArray.length == 0) {
            return "";
        }
        return new String(byteArray, Charset.defaultCharset());
    }


    /**
     *byte[] to float
     * @param floatArray
     * @return
     */
    public static float bytes2float(byte[] floatArray){
        int accum = 0;
        for(int i = 0,j = 0; i < floatArray.length; i + + ){
            accum = accum|(floatArray[i] & amp; 0xff) << j;
            j + = 8;
        }
        return Float.intBitsToFloat(accum);
    }


    /**
     * byte[] to double
     * @param doubleArray
     * @return
     */
    public static double bytes2double(byte[] doubleArray) {
        long value = 0;
        for (int i = 0; i < doubleArray.length; i + + ) {
            value |= ((long) (doubleArray[i] & amp; 0xff)) << (8 * i);
        }
        return Double.longBitsToDouble(value);
    }

    /**
     * Convert array to hexadecimal string
     * @param array
     * @return HexString
     */
    public static String bytes2HexStr(byte[] array) {
        StringBuffer sb = new StringBuffer(array.length);
        String sTemp;
        for (int i = 0; i < array.length; i + + ) {
            sTemp = Integer.toHexString(0xFF & amp; array[i]);
            if (sTemp.length() < 2){
                sb.append(0);
            }
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }

    /**
     * Convert the array into a hexadecimal string with 0x
     * @param array
     * @return HexString
     */
    public static String bytes2FullHexStr(byte[] array) {
        StringBuffer sb = new StringBuffer(array.length);
        sb.append("0x");
        String sTemp;
        for (int i = 0; i < array.length; i + + ) {
            sTemp = Integer.toHexString(0xFF & amp; array[i]);
            if (sTemp.length() < 2){
                sb.append(0);
            }
            sb.append(sTemp);
            if(i < array.length-1){
                sb.append("0x");
            }
        }
        return sb.toString().toLowerCase();
    }

    /**
     * short to hexadecimal string
     * @param num
     * @return
     */
    public static String short2HexStr(short num){
        return Integer.toHexString(num);
    }

    /**
     * Convert hexadecimal string into byte array
     * @param hex
     * @return byte[]
     */
    public static byte[] hexStr2Bytes(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;
    }

    /**
     * Convert the hexadecimal string with 0x into a byte array
     * @param hex
     * @return byte[]
     */
    public static byte[] fullHexStr2Bytes(String hex){
        hex = hex.toLowerCase().replaceAll("0x","").trim().toUpperCase();
        return hexStr2Bytes(hex);
    }

    private static int toByte(char c) {
        byte b = (byte) "0123456789ABCDEF".indexOf(c);
        return b;
    }

    /**
     * Convert byte array to time string format yyMMddHHmmss
     * @return
     */
    public static String bytes2timeStr(byte[] array){
        StringBuilder stringBuilder = new StringBuilder();
        for(int i = 0; i < array.length; i + + ){
            int timeUnit = byte2UnsignedInt(array[i]);
            if(timeUnit < 10){
                stringBuilder.append(0);
            }
            stringBuilder.append(timeUnit);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdf.format(new Date()).substring(0,2) + stringBuilder.toString();
    }


    /**
     * byte to unsigned integer
     * @param value
     * @return
     */
    public static int byte2UnsignedInt(byte value) {
        return Byte.toUnsignedInt(value);
    }

    /**
     *
     *
     * @Description Convert a long into an 8-bit byte[]
     * @param num
     * long value
     * @return
     * byte[] with length 8
     * @throwsException
     */
    public static byte[] longToBytes(long num) {
        byte[] b = new byte[8];
        for (int i = 0; i < 8; i + + ) {
            b[i] = (byte) (num >>> (56 - i * 8));
        }
        return b;
    }
    /**
     *
     *
     * @Description Convert an array into a long value
     * @param b
     * byte[] with length 8
     * @return
     * long value
     * @throwsException
     */
    public static long bytesToLong(byte[] b) {
        int mask = 0xff;
        long temp = 0;
        long res = 0;
        for (int i = 0; i < 8; i + + ) {
            res <<= 8;
            temp = b[i] & amp; mask;
            res |= temp;
        }
        return res;
    }

    /**
     * Convert a byte array into a binary string
     * @param bytes
     * @return binary string
     */
    public static String bytes2bitStr(byte[] bytes){
        StringBuilder stringBuilder = new StringBuilder();
        for (byte b : bytes) {
            stringBuilder.append(byte2bitStr(b));
        }
        return stringBuilder.toString();
    }

    /**
     * Convert a byte into a binary string
     * @param b
     * @return binary string
     */
    public static String byte2bitStr(byte b) {
        return "" + (byte) ((b >> 7) & amp; 0x1) + (byte) ((b >> 6) & amp; 0x1)
                 + (byte) ((b >> 5) & amp; 0x1) + (byte) ((b >> 4) & amp; 0x1)
                 + (byte) ((b >> 3) & amp; 0x1) + (byte) ((b >> 2) & amp; 0x1)
                 + (byte) ((b >> 1) & amp; 0x1) + (byte) ((b >> 0) & amp; 0x1);
    }

    public static byte bitToByte(String bit) {
        return (byte) bitToInt(bit);
    }

    public static int bitToInt(String bit) {
        int re, len;
        if (null == bit) {
            return 0;
        }
        len = bit.length();
        if (len != 4 & amp; & amp; len != 8) {
            return 0;
        }
        if (len == 8) {// 8 bit processing
            if (bit.charAt(0) == '0') {// Positive number
                re = Integer.parseInt(bit, 2);
            } else {// negative number
                re = Integer.parseInt(bit, 2) - 256;
            }
        } else {//4 bit processing
            re = Integer.parseInt(bit, 2);
        }
        return re;
    }
}