package com.siwei.recyclebox.deviceUtils;

public class ByteUtil {

    /**
     * 字节数组依次异或生成校验码
     * @param datas
     * @return
     */
    public static byte getXor(byte[] datas){
        byte temp=datas[1];
        for (int i = 2; i <datas.length-2; i++) {
            temp ^=datas[i];
        }
        return temp;
    }

    public static String bytesToString(byte[] bytes){
        if (bytes == null) {
            return null;
        }
        StringBuilder builder = new StringBuilder();
        char[] hexArray = "0123456789ABCDEF".toCharArray();
        for (byte aByte : bytes) {
            int v = aByte & 0xFF;
            builder.append(hexArray[v >>> 4]).append(hexArray[v & 0x0F]).append(" ");
        }
        return builder.toString();
    }

    public static byte[] generateBytes( int dataLen , byte[] data , byte[]sendBytes  ){
        for( int i = 0;i<dataLen;i++){
            sendBytes[5+i]=data[i];
        }
        return sendBytes;
    }

    /**
     * 输入的16进制字符串转换成bytes
     * @param hexString
     * @return
     */
    public static byte[] hexStringToBytes(String hexString) throws IllegalArgumentException {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.replaceAll(" ","");
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }
    private static byte charToByte(char c) throws IllegalArgumentException {
        byte index = (byte) "0123456789ABCDEF".indexOf(c);
        if(index == -1){
            throw new IllegalArgumentException(c+" 不是16进制数");
        }
        return index;
    }

    public static boolean isEmpty(String string){
        if(string == null || string.isEmpty())
            return true;
        return false;
    }

    public static byte[] short2byte(short s){
        byte[] b = new byte[2];
        for(int i = 0; i < 2; i++){
            int offset = 16 - (i+1)*8; //因为byte占4个字节，所以要计算偏移量
            b[i] = (byte)((s >> offset)&0xff); //把16位分为2个8位进行分别存储
        }
        return b;
    }
    public static byte[] int2shotybyte(int s){
        byte[] b = new byte[2];
        for(int i = 0; i < 2; i++){
            int offset = 16 - (i+1)*8; //因为byte占4个字节，所以要计算偏移量
            b[i] = (byte)((s >> offset)&0xff); //把16位分为2个8位进行分别存储
        }
        return b;
    }
    public static short byte2short(byte[] b){
        short l = 0;
        for (int i = 0; i < 2; i++) {
            l<<=8; //<<=和我们的 +=是一样的，意思就是 l = l << 8
            l |= (b[i] & 0xff); //和上面也是一样的  l = l | (b[i]&0xff)
        }
        return l;
    }
    public static byte[] int2byte(int s){
        byte[] b = new byte[4];
        for(int i = 0; i < 4; i++){
            int offset = 32 - (i+1)*8; //因为byte占4个字节，所以要计算偏移量
            b[i] = (byte)((s >> offset)&0xff); //把32位分为4个8位进行分别存储
        }
        return b;
    }
    public static int bytes2Int(byte[] bytes) {
        int value = 0;
        // 由高位到低位
        for (int i = 0; i < 4; i++) {
            int shift = (4 - 1 - i) * 8;
            value += (bytes[i] & 0x000000FF) << shift;// 往高位游
        }
        return value;
    }

}
