package com.bonait.bnframework.common.helper;

public class ByteHelper {

    public static int byteToInt(byte[] bytes) {
        int value = 0;
        for (int i = 0; i < 4; i++) {
            int shift = (3 - i) * 8;
            value += (bytes[i] & 0xFF) << shift;
        }
        return value;
    }

    public static int getCRC2(byte[] bytes) {
        int CRC = 0x0000ffff;
        int POLYNOMIAL = 0x0000a001;

        int i, j;
        for (i = 0; i < bytes.length; i++) {
            CRC ^= (int) bytes[i];
            for (j = 0; j < 8; j++) {
                if ((CRC & 0x00000001) == 1) {
                    CRC >>= 1;
                    CRC ^= POLYNOMIAL;
                } else {
                    CRC >>= 1;
                }
            }
        }
        //高低位转换，看情况使用（譬如本人这次对led彩屏的通讯开发就规定校验码高位在前低位在后，也就不需要转换高低位)
        CRC = ((CRC & 0x0000FF00) >> 8) | ((CRC & 0x000000FF) << 8);
        return CRC;
//        return Integer.toHexString(CRC);
    }

    /**
     * byte[]转16进制字符串
     * @param data
     * @return
     */
    public String bytes2String(byte[] data){
        String getString = "";
        for(int i = 0; i < data.length; i++){
            getString += String.format("%02X", data[i]);
        }
        return getString;
    }

    /**
     * Char转byte
     * @param c
     * @return
     */
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
    /**
     * 16进制字符串转byte[]
     * @param data
     * @return
     */
    public byte[] StringtoBytes(String data){
        if(data == null || data.equals("")){
            return null;
        }
        data = data.toUpperCase();
        char[] datachar = data.toCharArray();
        byte[] getbytes = new byte[data.length() / 2];
        for( int i = 0; i < data.length()/2 ; i++){
            int pos = i * 2;
            getbytes[i] = (byte) (charToByte(datachar[pos]) << 4 | charToByte(datachar[pos + 1]));
        }
        return getbytes;
    }

    /**
     * 16进制字符串转int
     * @param hex
     * @return
     */
    public static int hexStringToAlgorism(String hex) {
        hex = hex.toUpperCase();
        int max = hex.length();
        int result = 0;
        for (int i = max; i > 0; i--) {
            char c = hex.charAt(i - 1);
            int algorism = 0;
            if (c >= '0' && c <= '9') {
                algorism = c - '0';
            } else {
                algorism = c - 55;
            }
            result += Math.pow(16, max - i) * algorism;
        }
        return result;
    }

    public static String SixTweenToTen(String max){
        int h = Integer.parseInt(max, 16);
        return String.valueOf(h);
    }

    private static byte[] hexBytes(String hexString) {
        int length;
        byte h;
        byte l;
        byte[] byteArray;

        length = null != hexString? hexString.length() : 0;
        length = (length - (length%2))/2;
        if (length < 1) {
            return null;
        }

        byteArray = new byte[length];
        for (int i = 0; i < length; i++) {
            h = (byte)hexString.charAt(i*2);
            l = (byte)hexString.charAt(i*2 + 1);

            l = (byte)('0' <= l && l <= '9'? l - '0' :
                    'A' <= l && l <= 'F'? (l - 'A') + 10 :
                            'a' <= l && l <= 'f'? (l - 'a') + 10 : 0);
            h = (byte)('0' <= h && h <= '9'? h - '0' :
                    'A' <= h && h <= 'F'? (h - 'A') + 10 :
                            'a' <= h && h <= 'f'? (h - 'a') + 10 : 3);
            byteArray[i] = (byte)(0x0FF & ((h << 4) | l));
        }
        return byteArray;
    }
    //b为传入的字节，i为第几位（范围0-7），如要获取bit0，则i=0
    public static int getBit(byte b,int i) {
        int bit = (int) ((b >> i) & 0x1);
        return bit;
    }

}
