CRC Program In C | CodeTextPro - CodeTextPro | Web Tutorial | Technology

CodeTextPro.com Provides - Web design, Technology gyan, Digital marketing, Online earnings, Seo, Adsense, Technology news, Motivation and inspiration, Latest Technology Affairs, Gadgets, Blogging

Wednesday, January 16, 2019

CRC Program In C | CodeTextPro

CRC in C


A Cyclic Redundancy Check (CRC) is an error-detecting code commonly used in digital networks and storage devices to detect accidental changes to raw data. Blocks of data entering these systems get a short check value attached, based on the remainder of a polynomial division of their contents. On retrieval, the calculation is repeated and, in the event the check values do not match, corrective action can be taken against data corruption. CRCs can be used for error correction.

CRC program:

#include<stdio.h>
#include<string.h>
#define N strlen(g)
char t[28],cs[28],g[]="10100101";
   int a,e,c,pos;
   void xor(){
     for(c=1;c<N;c++)
     cs[c]=((cs[c]==g[c])?'0':'1');//turnary for xor operation
    }
    void crc(){
      for(e=0;e<N;e++)
           cs[e]=t[e];
         do{
            if(cs[0]=='1')
              xor(); 
      for(c=0;c<N;c++)
          cs[c]=cs[c+1];
         cs[c]=t[e++];
       }while(e<=a+N-1);
     }
  int main() 
  {
    int flag=0;
    unsigned char x='A';
    int i;
       printf("\nEnter data :");
       scanf("%s",t);
       printf("\n");
       printf("\nGenerating polynomial : %s",g);
       a=strlen(t);
       for(e=a;e<a+N-1;e++)
            t[e]='0';
       printf("\n                 ");
       printf("\nModified data is : %s",t);
       printf("\n____________________________");
       crc();
       printf("\nChechsum is : %s",cs);
       for(e=a;e<a+N-1;e++)
           t[e]=cs[e-a];
       printf("\n_________________________");
       printf("\nFinal codeword is : %s",t);
       printf("\n_________________________");
       printf("\nTest error detection 0(yes) 1(no)? : ");
       scanf("%d",&e);
       if(e==0)
       {
           printf("\nEnter the position where error is to be inserted :");
           scanf("%d",&pos);
           t[pos-1]=(t[pos-1]=='0')?'1':'0';
           printf("\n________________________");
           printf("\nErrorneous data : %s\n",t);
           }
             crc();
             for(e=0;e<N-1;e++)
           {
              if(cs[e]=='1')
              {
                 flag=1;
                 break;
                 }
              }
          if(flag==1)
          printf("\nError detected\n\n");
          else
          printf("\nNO error");
          return 0;
          }                  
                        

No comments:

Post a Comment