Created
November 1, 2015 17:32
-
-
Save rajabishek/a045f7fd624e916f97e1 to your computer and use it in GitHub Desktop.
Cyclic Redundancy Check
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<string.h> | |
#define N strlen(g) | |
char t[28],cs[28],g[]="1011"; | |
int a,e,c; | |
void xorfunction(){ | |
for(c = 1;c < N; c++) | |
cs[c] = (( cs[c] == g[c])?'0':'1'); | |
} | |
void crc(){ | |
for(e=0;e<N;e++) | |
cs[e]=t[e]; | |
do{ | |
if(cs[0]=='1') | |
xorfunction(); | |
for(c=0;c<N-1;c++) | |
cs[c]=cs[c+1]; | |
cs[c]=t[e++]; | |
}while(e<=a+N-1); | |
} | |
int main() | |
{ | |
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("\nPaded data is : %s",t); | |
printf("\n----------------------------------------"); | |
crc(); | |
printf("\nCRC is : %s",cs); | |
for(e=a;e<a+N-1;e++) | |
t[e]=cs[e-a]; | |
printf("\n----------------------------------------"); | |
printf("\nFinal data to be sent : %s",t); | |
printf("\n----------------------------------------"); | |
printf("\nTest error detection 0(yes) 1(no)? : "); | |
scanf("%d",&e); | |
if(e==0) | |
{ | |
do{ | |
printf("\nEnter the position where error is to be inserted : "); | |
scanf("%d",&e); | |
}while(e==0 || e>a+N-1); | |
t[e-1]=(t[e-1]=='0')?'1':'0'; | |
printf("\n----------------------------------------"); | |
printf("\nErroneous data : %s\n",t); | |
} | |
crc(); | |
for(e=0;(e<N-1) && (cs[e]!='1');e++); | |
if(e<N-1) | |
printf("\nError detected\n\n"); | |
else | |
printf("\nNo error detected\n\n"); | |
printf("\n----------------------------------------\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment