Tuesday, November 10, 2015

CRC calculation in C

Here are some useful Internet resources when you have to deal with CRC calculation and checking. I found them handy and use occasionally. Some are online calculators, some gives also technical descriptions.
Below is some real working C source code. Borrowed from here!
It is tested and working. It calculates CRC CCITT16 with starting value 0xFFFF. It might be not as fast as table based CRC calculations, but occupies less RAM and code space which sometimes is essential for embedded systems.

#define POLY 0x1021

unsigned char i;
unsigned int crc = 0xFFFF;

while (wLength--) {
    wCrc ^= *(unsigned char *)pData++ << 8;
    for (i=0; i < 8; i++)
        wCrc = wCrc & 0x8000 ? (wCrc << 1) ^ POLY : wCrc << 1;
return wCrc & 0xffff;

No comments:

Post a Comment