美文网首页
常用代码

常用代码

作者: 云中月a | 来源:发表于2016-09-07 15:49 被阅读0次
    • 异或运算加密
    /**加密算法**/
    private static short encryption(short msgId, short mesType) {
        short y;
        short desCode;
        y = (short) ((msgId % 10 * 10 + msgId % 100 / 10 + mesType * 2) % 100);
        System.out.println("y:" + y);
        desCode = (short) BLE_Cipher[y];
        return desCode;
    }
    
    • CRC8校验
    /**
     * CRC8校验码
     * @param buffer
     * @param key
     * @return
     */
    public static byte getCRCR8(byte[] buffer, short key) {
        short i;
        byte crc;
        crc = 0;
        int ptr = 0;
        int len;
        len = buffer.length;
        while (len-- != 0) {
            for (i = 0x80; i != 0; i >>= 1) {
                if ((crc & 0x80) != 0) {
                    crc <<= 1;
                    crc ^= key;
                } else {
                    crc <<= 1;
                }
                if ((buffer[ptr] & i) != 0) {
                    crc ^= key; /* 校验模数 */
                }
            }
            ptr++;
        }
        return crc;
    }
    

    相关文章

      网友评论

          本文标题:常用代码

          本文链接:https://www.haomeiwen.com/subject/trvjettx.html