java實現CRC16_X25算法,參考c語言移植過來,解決數據溢出問題

public static int getCrc_CRC16_X25(byte[] bytes) {
    int i, j, lsb;
    int h = 0xffff;
    for (i = 0; i < bytes.length; i++) {
        h ^= (bytes[i] & 0xff);
        h &= 0xffff;
        for (j = 0; j < 8; j++) {
            lsb = h & 0x0001; //取 CRC 的移出位
            h >>= 1;
            if (lsb == 1) {
                h ^= 0x8408;
                h &= 0xffff;
            }
        }
    }
    h ^= 0xffff;
    return h;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章