[c language] simulating manual CRC check code

Source: Internet
Author: User

The course design of composition principle is to realize CRC code generation and verification, but it will not be implemented by hardware ...

Had to use C to write to play, do the problem can also use ... The net original to test ...

Example: the data to be sent is 1101011011,CRC to generate the polynomial P (X) =x4+x+1, which should be added to the remainder after the Data.

Written calculation process:

Programming calculation:

Enclose the small white code ...

1#include"stdafx.h"2 #defineDatabit_length 10//data Bits3 #defineGenerator_length 5//generation of polynomial digits4 #defineDatacrc_length databit_length+generator_length-15 6 intMain ()7 {8     intdatabit[databit_length]; Data9     intcrcbit[generator_length-1]; RemainderTen      for(inti =0; I < generator_length-1; i++)//remainder initialized to 0 one     { acrcbit[i] =0; -     } -     intgenerator[generator_length] = {1,0,0,0,1}; Initializing the build polynomial theprintf"Enter the resulting polynomial:"); -      for(inti =0; I < generator_length; i++) -     { -scanf"%d", &generator[i]); +     } -      for(inti =0; I < databit_length; i++)//initialize the data to be transferred +     { adatabit[i] =0; at     } -printf"input data calculation check bit:"); -      for(inti =0; I < databit_length; i++) -     { -scanf"%d",&databit[i]); -     } in     intdatacrc[datacrc_length]; 0 after data -      for(inti =0; I < datacrc_length; i++) to     { +         if(i <Databit_length) -         { thedatacrc[i] =databit[i]; *         } $         ElsePanax Notoginsengdatacrc[i] =0; -     } the     intcrctmp[generator_length]; "minuend" in the process of modulo 2, "meiosis" is the generation of a polynomial +      for(inti =0; I < generator_length; i++) a     { thecrctmp[i] =-1; The beginning of the meiosis, let each one be -1 . +     } -     intp =0; $      for(inti = p; I < datacrc_length; i++) $     { -         if(crctmp[0] == -1) -         { the              for(intK =0; K < generator_length-1; k++) -             {Wuyicrctmp[k] = Crctmp[k +1]; If the first digit of the meiosis is-1, move one left and one on the Right.  the             } -crctmp[generator_length-1] =datacrc[i]; One of the vacated one reads a number from the Top. wu         } -         if(crctmp[0] != -1)//when The first digit is not-1 about         { $         //printf ("read"); -              for(intj =0; J < generator_length; J + +)//prove that reading a length is as long as generating a polynomial, you can do modulo 2 operation.  -             { -printf"%d", crctmp[j]); Output is subtracted a             } +printf"\ n"); the              for(intj =0; J < generator_length; J + +) -             { $printf"%d", generator[j]); Output meiosis the             } theprintf"\ n"); theprintf"\ n"); the         //printf ("count \ n"); -              for(intj =0; J < generator_length; J + +)       in             { thecrctmp[j] = crctmp[j] ^generator[j]; Bitwise XOR or get results the             } about              for(intj =0; J < generator_length;j++) the             { the                 if(crctmp[j] = =1) the                      break; +crctmp[j] =-1; Put the above results in front of a few 0 to 1, loop back to continue Reading.  -             } the         }Bayi         //for (int j = 0; J < generator_length; J + +) the         //{ the         //printf ("%d", crctmp[j]); -         //} -         //printf ("\ n"); the     } the      for(inti =1; I < generator_length; i++) the     { the         if(crctmp[i]! =-1&&crctmp[i]!=0)//dividend All Fall down, get the remainder, and deposit into the Crcbit.  -crcbit[i-1] =1; the     } theprintf"remainder:"); the      for(inti =0; I < generator_length-1; i++)94     { theprintf"%d", crcbit[i]); the     } the getchar (); GetChar ();98     return 0; about}

C + + can be written in the Bitset class, it is not necessary to complete these arrays ...

[c language] simulating manual CRC check code

Related Article

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.