CompuServe Messages

16-bit CRC

    22-Jan-92 01:55:39
Sb: #19285-16-bit CRC
Fm: Don Curtis/SYSOP 76703,4321
To: Michael A McCormick 76046,1057
Mike, Here's the explanation from an old Dr. Dobbs on how to generate a CRC for XMODEM transfers. It's using the CCITT X*16 + X*12 + X*5 +1 as it's polynomial. The procedure for generating the CRC is this: A byte to be added to the CRC is fed into the generator, one bit at a time, high bit first. The bit is shifted into a 16-bit CRC accumulator low end. If the high bit shifted out of the CRC accumulator is a 1, the CRC accumulator is exclusive ORed with 0x1021 (the polynomial). The process repeats for all eight bits of the input character. At the end, 2 zero bytes are sent thru the accumulator to flush the last 2 actual characters thru the process. The code is: unsigned crcaccum; VOID updt_crc(x) char x; { unsigned shifter, i, flag; for (shifter = 0x80; shifter ;shifter >> 1 ) { flag = (crcaccum & 0x8000); /* is the high bit set ?*/ crcaccum << 1; /* shift right */ crcaccum |= ((shifter & x) ? 1 : 0); /* add in bit from x */ if (flag) { /* was the high bit set ? */ crcaccum ^= 0x1021; /* xor with the polynomial */ } } } Don