Implementation of CRC Checksum

Source: Internet
Author: User

"For a long time no More blog:: >_<::"

Computer network class learned the basic principle of CRC School (Jiao) test, but the lesson of that little thing only enough to do the exam question = =, so want to try to write a file CRC check the small program

First, review the most basic principles of CRC calibration:

Core idea: In fact, and hash very much like ... It's mod.

Set M is the source file, G is the number of checks used (specified by the CRC standard)

First, m passes the checksum to N to satisfy the N mod g==0 (where mod is the modulo two not carry division of the textbook)

Then send n over the network and receive the packet Q in destination

Destination after receiving also check: if the Q mod g==0, the documentation is correct, otherwise the error

Of course, this method will also exist in the case of missed (such as n=q+k*g when it was accepted, but in fact is wrong).

Choosing the right G can reduce the missed rate (just like when a hash is used to select a large prime number).

The basic CRC algorithm is this:

Set G with R bit

STEP1: r-1 0 at the end of M, becomes XRM

STEP2: Modulo two non-carry division (in fact, the vertical in addition to the Mrs. Fremont to replace the XOR)

[XrM] mod [G], take remainder remainder

STEP3: Make N=xrm-remainder,

The resulting n is a well-calibrated packet. Send it straight out.

In the process above, remainder is the CRC check code

Where modulo two does not carry a detailed process of division:

Program implementation:

Current bit =15→0 in for data
The highest bit of tr=register
Register shift left One
Inserts the current bit in data into the lowest bit of the register
If TR is 1:
Register=register XOR Poly

1 #Load The register with zero bits.2 #Augment the message by appending W zero bits to the end of it.3 #While (more message bits)4 #Begin5 #Shift The register left by one bit, reading the next bit of the6 #augmented message into register bit position 0.7 #If (a 1 bit popped out of the register during step 3)8 #Register = Register XOR Poly.9 #EndTen #The register now contains the remainder. One  Adata=0x35b -Poly=0x13 -Dbit=16 thePbit=5 -regi=0x0 -  -data=data<< (pbit-1) +Dpoint=dbit-1 -  while(dpoint>=0): +Tr= (regi>> (pbit-2)) &0x1#MSB in Register ARegi=regi<<1 attmp= (Data>>dpoint) &0x1 -regi+=tmp -     if(tr==1): -regi=regi^Poly -Dpoint=dpoint-1 -  in Print(Regi)
View Code

The above method is the most bare method ... A bit at a bit to handle.

But if you do it in practice, it's too slow. =

Implementation of CRC Checksum

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.