How CRC algorithms work for CCITT16 and how to get them for CCITT8

I'm looking for a CRC-8 checksum implementation - and while reading on CRC in general I came across this algorithm for CCITT-16 (polynomial X ^ 16 + X ^ 12 + X ^ 5 + 1):

unsigned char ser_data;
static unsigned int crc;

crc  = (unsigned char)(crc >> 8) | (crc << 8);
crc ^= ser_data;
crc ^= (unsigned char)(crc & 0xff) >> 4;
crc ^= (crc << 8) << 4;
crc ^= ((crc & 0xff) << 4) << 1;

      

Alternatively as a macro:

#define crc16(chk, byte)                                   \
        {                                                  \
          chk = (unsigned char) (chk >> 8) | (chk << 8);   \
          chk ^= byte;                                     \
          chk ^= (unsigned char)(chk & 0xFF) >> 4;         \
          chk ^= (chk << 8) << 4;                          \
          chk ^= ((chk & 0xFF) << 4) << 1;                 \
        }

      

I have two questions:

  • How is this algorithm derived from a polynomial?
  • Is there a similar simple algorithm for CCITT8 (polynomial X ^ 8 + X ^ 2 + X + 1)?
+3


source to share


3 answers


See this painless guide to CRC error detection algorithms



+3


source


Here is a C CRC8-CCITT implementation based on the code from this answer ( fooobar.com/questions/327864 / ... ):



uint8_t crc8_ccitt(uint8_t crc, const uint8_t *data, size_t dataLength){
    static const uint8_t POLY = 0x07;
    const uint8_t *end = data + dataLength;

    while(data < end){
        crc ^= *data++;
        crc = crc & 0x80 ? (crc << 1) ^ POLY : crc << 1;
        crc = crc & 0x80 ? (crc << 1) ^ POLY : crc << 1;
        crc = crc & 0x80 ? (crc << 1) ^ POLY : crc << 1;
        crc = crc & 0x80 ? (crc << 1) ^ POLY : crc << 1;
        crc = crc & 0x80 ? (crc << 1) ^ POLY : crc << 1;
        crc = crc & 0x80 ? (crc << 1) ^ POLY : crc << 1;
        crc = crc & 0x80 ? (crc << 1) ^ POLY : crc << 1;
        crc = crc & 0x80 ? (crc << 1) ^ POLY : crc << 1;
    }

    return crc;
}

      

0


source


this webpage: https://decibel.ni.com/content/docs/DOC-11072 contains a link to a .zip file for each of the common (including the ones you are asking for) crc calculation algorithms.

0


source







All Articles