search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

純C的 CRC16(和 C# CRC16 CCITT TABLE 範例 運算結果相同)運算 – jashliao部落格

純C的 CRC16(和 C# CRC16 CCITT TABLE 範例 運算結果相同)運算

 

資料來源:https://github.com/jash-git/C_CRC16

#include
#include
/*
Figure 2.7 ¡V C Language implementation of the CRC calculation
*/
unsigned int calcrc16( unsigned char * dataP, int n )
{
    unsigned char i, j; // Byte counter, bit counter
    unsigned int crc16; // calculation
    crc16 = 0x0000; // PRESET value
    for (i = 0; i < n; i++) // check each Byte in the array
    {
        crc16 ^= *dataP++;
        for (j = 0; j < 8; j++) // test each bit in the Byte
        {
            if(crc16 & 0x0001 )
            {
                crc16 >>= 1;
                crc16 ^= 0x8408; // POLYNOMIAL x^16 + x^12 + x^5 + 1
            }
            else
            {
                crc16 >>= 1;
            }
        }
    }
    return( crc16 ); // returns calculated crc (16 bits)
}
int main()
{
    unsigned char data[6];
    //string input = “00 06 80 01 00 03”;
    data[0]=00;
    data[1]=06;
    data[2]=16*8;
    data[3]=01;
    data[4]=00;
    data[5]=02;
    unsigned int CRC16= calcrc16( data, 6 );
    printf(“%d\n%X”,CRC16,CRC16);
    return 0;
}

 

 

 

 

 



熱門推薦

本文由 jashliaoeuwordpress 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦