CRC-16/CRC-32 Program code

Source: Internet
Author: User

CRC-16 is used to write a program not long ago, but looking for to find only in Uddf found a Delphi CRC-32 program code, but also use look-up table method, although the speed of look-up table method, but 256 32-bit data I suspect there may be input errors, people are not so relieved, and I do not know this How the table was calculated. Then I found a section about CRC in a notebook two years ago, also do not know where to copy from, fortunately there is a section of the program code, is CRC-16, this procedure is to produce CRC table, but this is just a few lines of the program (basically with the following BuilderTable16 function is the same) see my confused, until these two days to understand, and the introduction of the CRC-32 algorithm, now all the procedures listed below, and make some explanations to help understand, not only to know it, but also to know why:

//Note: Because the highest position must be "1", so omit


Const unsigned short cncrc_16 = 0x8005;


//CRC-16 = X16 + X15 + X2 + X0


Const unsigned short Cncrc_ccitt = 0x1021;


//Crc-ccitt = X16 + X12 + X5 + X0, it is said that this 16-bit CRC polynomial is better than the previous


Const unsigned long cncrc_32 = 0X04C10DB7;


//CRC-32 = X32 + X26 + X23 + X22 + X16 + X11 + X10 + X8 + X7 + X5 + X4 + X2 + X1 + X0


unsigned long table_crc[256]; CRC Table


//Constructed 16-bit CRC table


void BuildTable16 (unsigned short apoly)


{


unsigned short i, J;


unsigned short ndata;


unsigned short naccum;


for (i = 0; i < 256; i++)


{


ndata = (unsigned short) (I << 8);


naccum = 0;


for (j = 0; J < 8; j + +)


{


if ((ndata ^ naccum) & 0x8000)


naccum = (naccum << 1) ^ apoly;


Else


naccum <<= 1;


ndata <<= 1;


}


Table_crc[i] = (unsigned long) naccum;


}


}


//Calculates 16-bit CRC value, CRC-16 or Crc-ccitt


unsigned short crc_16 (unsigned char * adata, unsigned long asize)


{


unsigned long i;


unsigned short naccum = 0;


BuildTable16 (CNCRC_16); or Cncrc_ccitt


for (i = 0; i < asize; i++)


Naccum = (naccum << 8) ^ (unsigned short) table_crc[(naccum >> 8) ^ *adata++];


return naccum;


}


//Constructed 32-bit CRC table


void BuildTable32 (unsigned long apoly)


{


unsigned long I, J;


unsigned long ndata;


unsigned long naccum;


for (i = 0; i < 256; i++)


{


ndata = (unsigned long) (I << 24);


naccum = 0;


for (j = 0; J < 8; j + +)


{


if ((ndata ^ naccum) & 0x80000000)


naccum = (naccum << 1) ^ apoly;


Else


naccum <<= 1;


ndata <<= 1;


}


Table_crc[i] = naccum;


}


}


//Calculate 32-bit CRC-32 value


unsigned long crc_32 (unsigned char * adata, unsigned long asize)


{


unsigned long i;


unsigned long naccum = 0;


BuildTable32 (CNCRC_32);


for (i = 0; i < asize; i++)


naccum = (naccum << 8) ^ table_crc[(naccum >>) ^ *adata++];


return naccum;


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.