2017-01-31 186 views
0

CRC校驗和的機制或步驟很容易,但軟件在某種程度上很多complicated和軟件中有一些步驟不兼容CRC的步驟 下面的圖片是步驟用於獲取CRC的校驗和(其僅僅是一個模2分割):CRC代碼和實現兼容性

的校驗和是餘數= 001
用於計算CRC校驗和的軟件是用於比特串是:

/* 
* The width of the CRC calculation and result. 
* Modify the typedef for a 16 or 32-bit CRC standard. 
*/ 
typedef uint8_t crc; 

#define WIDTH (8 * sizeof(crc)) 
#define TOPBIT (1 << (WIDTH - 1)) 

crc 
crcSlow(uint8_t const message[], int nBytes) 
{ 
    crc remainder = 0; 


    /* 
    * Perform modulo-2 division, a byte at a time. 
    */ 
    for (int byte = 0; byte < nBytes; ++byte) 
    { 
     /* 
     * Bring the next byte into the remainder. 
     */ 
     remainder ^= (message[byte] << (WIDTH - 8)); 

     /* 
     * Perform modulo-2 division, a bit at a time. 
     */ 
     for (uint8_t bit = 8; bit > 0; --bit) 
     { 
      /* 
      * Try to divide the current data bit. 
      */ 
      if (remainder & TOPBIT) 
      { 
       remainder = (remainder << 1)^POLYNOMIAL; 
      } 
      else 
      { 
       remainder = (remainder << 1); 
      } 
     } 
    } 

    /* 
    * The final remainder is the CRC result. 
    */ 
    return (remainder); 

} 

我看到有不兼容性在part(remainder<<1)的軟件,因爲這些移動將始終在正確的加0,即使下面的位不爲0

,並在部分: remainder ^= (message[byte] << (WIDTH - 8));

把我穿上」的第一個字節時t看到的問題,因爲初始值是因爲初始值爲0,但是當插入下一個字節時,爲什麼我們將它們的每個字節與先前的餘數相比較

+0

沒有** ** CRC。有非常不同的CRC算法。閱讀[問]並按照建議。 – Olaf

+0

什麼是「模2分」? CRC不是。有一些好的文章描述了CRC的概念及其實現。你還應該閱讀[問]!哦,你的代碼可以調用'int'和'crc'的sizese組合的未定義行爲。 – Olaf

回答

0

代碼示例似乎使用可變大小的CRC,其中大小的CRC是WIDTH。多項式是WIDTH + 1位多項式的底部WIDTH位,它將最低有效位設置爲1.由於操作是XOR,因此數據位異或的順序並不重要,所以8個數據位可以同時被異或爲高位。然後該位在一個時間反饋週期中發生8位。由於多項式的最低位是1,只要數據中有1位,就會保持循環。