CRC16 Implementation Code
Idea: Take a character (8bit), check the character bit by digit, if it is 1,crc^crc_mul, and if the original CRC highest bit is 1, then crc^crc_mul the left 1 bits, otherwise it just moves left one bit. After calculating one character, load the next character.
#include <stdio.h>#defineCrc_mul 0x1021//generating polynomialunsignedintCAL_CRC (unsignedChar*ptr, unsignedCharLen) {unsignedChari; unsignedintCrc=0; while(len--! =0) { for(i=0x80; i!=0; i>>=1) { if((crc&0x8000)!=0) {CRC<<=1; CRC^=(Crc_mul); }Else{CRC<<=1; } if((*ptr&i)! =0) {CRC^=(Crc_mul); }} PTR++; } return(CRC);}intMain () {unsignedChari[8] = {0x00,0x00,0x00,0x00,0x06,0x0d,0xd2,0xe3}; unsignedintCRC; CRC=CAL_CRC (I,8); return 0;}
CRC32 Coded character Tabulation
#include <stdio.h>unsignedintcrc32_table[ the] = {0};voidinit_crc32_table () { for(inti =0; I! = the; i++) {unsignedintCRC =i; for(intj =0; J! =8; J + +) { if(CRC &1) CRC= (CRC >>1) ^0xedb88320; ElseCRC>>=1; } Crc32_table[i]=CRC; }}unsignedintGETCRC32 (unsignedChar* buf, unsignedintLen) {unsignedintCrc32_data =0xFFFFFFFF; for(unsignedinti =0; I! = Len; ++i) {unsignedintt = (crc32_data ^ buf[i]) &0xFF; Crc32_data= ((Crc32_data >>8) &0xFFFFFF) ^Crc32_table[t]; } return~Crc32_data;}intMain () {unsignedChari[8] = {0x00,0x00,0x00,0x00,0x06,0x0d,0xd2,0xe3}; Init_crc32_table (); printf ("BUFFER i ' s crc32:0x%x\n", GetCRC32 (i,8)); printf ("CRC32 table:\n"); for(intI=0;i< the; i++) {printf ("0x%8x\t", Crc32_table[i]); if((i+1)%8==0) printf ("\ n"); }}
Manual Calculation example of CRC check code
Generate Polynomial: G (X) =x4+x3+1, requires the CRC checksum of the binary sequence 10110011.
(1) G (x) =x4+x3+1, binary bit string is 11001, (there are several sides of X, corresponding 2 of the bits of a few square is 1)
(2) Because the check code 4 bits, so 10110011 and then add 4 0, get 101100110000, with "Modulo 2 Division" (In fact, or ^) can be obtained results;
Figure 5-10 CRC check Code calculation example
(3) crc^101100110000 get 101100110100. Sent to the receiving end;
(4) The receiving end receives 101100110100 divided by 11001 (by "Modulo 2 Division" method to remove), the remainder of 0 is error-free;
CRC Calibration Principle
After the K-bit information code and then splicing the R-bit check code, the message encoding length is n bits, therefore, this code is called (n,k) code.
Theorem: for a given (n,k) code, it can be proved that there is a maximum power of n=k+r the polynomial G (x) , exists and there is only one R Sub- polynomial G (x) , making .
which
M (x): k-th information polynomial,
R (x): R-1-times Check polynomial,
g (x): Generate polynomial:.
The sender generates a CRC check code for R bit by the specified g (x), and the receiver verifies that the CRC check code of the received message code is 0 by this g (X).
Assuming that the message is sent with information polynomial C (x), the C (x) is shifted to the left R bit, it can be represented as C (x) *2R, so that the right side of C (x) will be vacated the position of R-bit check code, Division (modulo 2), the remainder R is the checksum code. The CRC code sent is to verify that the received message encoding is correct, still doing modulo 2 except:.
Generation polynomial of CRC
The selection of the resulting polynomial should meet the following criteria:
A, the highest and lowest bits of the generated polynomial must be 1.
b, when the transmitted information (CRC code) any one error, is generated polynomial modulo 2 after the addition, should make the remainder is not 0.
C, different bits when errors, should make the remainder different.
D, the remainder to continue to do modulo 2 in addition, should make the remainder cycle.
The main generation polynomial g (x) has the following types:
Name |
Generating a polynomial |
Numerical formula |
Précis-writers Type |
Standard reference |
CRC-16 |
X16+x15+x2+1 |
0x1 ' 8005 |
8005 |
IBM SDLC |
Crc-ccitt |
X16+x12+x5+1 |
0x1 ' 1021 |
0x1021 |
ISO Hdlc,itu X.25,v.34/v.41/v.42,ppp-fcs |
CRC-32 |
Note |
0x1 ' 04c11db7 |
0x04c11db7 |
ZIP,RAR,IEEE 802 Lan/fddi,ieee1394,ppp-fcs |
Note * x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1
The resulting polynomial g (x) in the following table is also common:
Name |
Generating a polynomial |
Numerical formula |
Précis-writers Type |
Standard reference |
CRC-4 |
X4+x+1 |
0x1 ' 3 |
0x3 |
ITU g.704 |
CRC-8 |
X8+x5+x4+1 |
0x1 ' 31 |
0x31 |
|
CRC-8 |
X8+x2+x1+1 |
0x1 ' 07 |
0x07 |
|
CRC-8 |
X8+x6+x4+x3+x2+x1 |
0x1 ' 5E |
0x5E |
|
CRC-12 |
X12+x11+x3+x2+x+1 |
0x1 ' 80F |
0x80f |
|
crc-32c |
NOTE * * |
0x1 ' 1edc6f41 |
0x1edc6f41 |
Sctp |
Note * * x32+x28+x27+x26+x25+x23+x22+x20+x19+x18+x14+x13+x11+x10+x9+x8+x6+1
CRC check code principle, example, manual calculation