Result: The CRC32 algorithm is correct.

Source: Internet
Author: User
Tags crc32

The following is a CRC32 computingCodeAccording to my tests, the results are correct. I hope to help you and avoid detours !!!

Note !!!!
The generated polynomial is: Poly = 0x4c11db7;

Class iso_13818_crc32 {
Public:
Iso_13818_crc32 ();
Void Update (char * data, long length );
Unsigned long get_value ();
PRIVATE:
Void process_byte (unsigned char B );
Unsigned char pump_bit (unsigned char B, int bit_pos );
Void decode_crc (unsigned char bit );

Unsigned long crc_reg;
Unsigned long poly;

};

Iso_13818_crc32: iso_13818_crc32 ()
{
Crc_reg = 0 xffffffff;
Poly = 0x4c11db7;
}

Void iso_13818_crc32: decode_crc (unsigned char bit)
{
Unsigned char add;
Add = (crc_reg & 0x80000000 )? 1: 0) ^ bit; // The highest bit of crc_reg is different from that of bit.
Crc_reg <= 1;
 
If (ADD ){
Crc_reg ^ = poly;
}
}

Unsigned long iso_13818_crc32: get_value ()
{
Return crc_reg;
}

Void iso_13818_crc32: process_byte (unsigned char B)
{
For (INT I = 0; I <8; I ++ ){
Decode_crc (pump_bit (B, 7-I ));
}
}

Unsigned char iso_13818_crc32: pump_bit (unsigned char B, int bit_pos)
{// Return the 8-bit_pos position of B (from high to low)
If (bit_pos> = 0 & bit_pos <= 7 ){

If (B & (1 <bit_pos )){
Return 1;
}
Else {
Return 0;
}
}
Else {
Return 0;
}
}

Void iso_13818_crc32: Update (char * data, long length)
{
Crc_reg = 0 xffffffff;
 
For (INT I = 0; I <length; I ++ ){
Process_byte (data [I]);
}
}

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.