0% found this document useful (0 votes)
1K views

C Programming Source Code For CRC

The document describes a C program that implements cyclic redundancy check (CRC) to detect errors in data transmission. It takes in a data string and generator polynomial as input, calculates the checksum by dividing the data by the polynomial, and appends it to the end of the data to create the codeword. It then tests error detection by inserting an error and checking if the checksum detects it. The program outputs the various steps and results.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

C Programming Source Code For CRC

The document describes a C program that implements cyclic redundancy check (CRC) to detect errors in data transmission. It takes in a data string and generator polynomial as input, calculates the checksum by dividing the data by the polynomial, and appends it to the end of the data to create the codeword. It then tests error detection by inserting an error and checking if the checksum detects it. The program outputs the various steps and results.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

C Programming Source Code for CRC(Cyclic Redundancy Check)

==========================================
C Programming Source Code for CRC(Cyclic Redundancy Check)
==========================================
#include"stdio.h"
#include"math.h"
#include"conio.h"
int decm1(int,int *);
int decm(int,int *);
int rx[20];
void main()
{
int n,num,gx[20],dx[30],fx[20],i;
int app,k,m,no,val,j;
clrscr();
printf("Enter length of dx: ");
scanf("%d",&m);
printf("Enter the value of dx: "); /*INFORMATION TO BE TRANSMITTED*/
for(i=0;i<m;i++)
{
scanf("%d",&dx[i]);
}
printf("\nEnter length of gx: "); /*LENGTH OF GENERATOR*/
scanf("%d",&n);
printf("\nEnter the value of gx: "); /*GENERATOR POLYNOMIAL*/
for(i=0;i<n;i++)
{
scanf("%d",&gx[i]);
}
app=n-1;
k=m+app;
for(i=m;i<k;i++)
dx[i]=0;
printf("\nMODIFIED VALUE OF BITS TO BE TRANSMITTED: ");
for(i=0;i<k;i++)
printf("%d",dx[i]);
no=k-n;
for(i=0;i
{
if (gx[i]==dx[i])
rx[i]=0;
else
rx[i]=1;
}
while(no!=0)
{
rx[n]=dx[i];
if(rx[1]==0)
{

for(j=0;j<n;j++)
fx[j]=0;
for(j=0;j<n;j++)
{
if (fx[j]==rx[j+1])
rx[j]=0;
else
rx[j]=1;
}
printf("\n");
for(j=0;j<n;j++)
printf("%d",rx[j]);
}
else
{
for(j=0;j<n;j++)
{
if (gx[j]==rx[j+1])
rx[j]=0;
else
rx[j]=1;
}
printf("\n");
for(j=0;j<n;j++)
printf("%d",rx[j]);
}
no--;
i++;
}
printf("CRC BITS");
num=n;
for(i=k-1;i>0;i--)
{
num--;
if(num>0)
{
if(dx[i]==rx[num])
dx[i]=0;
else
dx[i]=1;
}
}
printf("\nTranssmitted bits: ");
for(i=0;i<k;i++)
printf("%d",dx[i]); /* ACTUAL BITS TRANSMITTED */
/*DIVIDING TRANSMITTED BIT BY GENERATOR BITS*/
for(i=0;i<n;i++)
{
if (gx[i]==dx[i])
rx[i]=0;

else
rx[i]=1;
}
for(i=0;i<n;i++)
printf("%d",rx[i]);
no=k-n;
while(no!=0)
{
rx[n]=dx[i];
if(rx[1]==0)
{
for(j=0;j<n;j++)
fx[j]=0;
for(j=0;j<n;j++)
{
if (fx[j]==rx[j+1])
rx[j]=0;
else
rx[j]=1;
}
printf("\n");
for(j=0;j<n;j++)
printf("%d",rx[j]);
}
else
{
for(j=0;j<n;j++)
{
if (gx[j]==rx[j+1])
rx[j]=0;
else
rx[j]=1;
}
printf("\n");
for(j=0;j<n;j++)
printf("%d",rx[j]);
}
no--;
i++;
}
printf("\nremainder bits:");
for(i=0;i<n;i++)
printf("%d",rx[i]);
for(i=0;i<n;i++)
{
if (rx[i]==0)
val=0;
else
val=1;
}
if(val==0)

printf("\n NO ERROR IN TRANSMISSION:");


else
printf("\n ERROR IN TRANSMISSION: ");
getch();
}</n;i++)
</n;i++)
</n;j++)
</n;j++)
</n;j++)
</n;j++)
</n;j++)
</n;i++)
</n;i++)
</k;i++)
</n;j++)
</n;j++)
</n;j++)
</n;j++)
</n;j++)
</k;i++)
</k;i++)
</n;i++)
</m;i++)

Cyclic redundancy check using C


?
1

//Program to add crc check bit

2
3

#include<stdio.h>

#include<string.h>

#define N strlen(g)

6
7

char t[28],cs[28],g[]="10001000000100001";

int a,e,c;

9
10

void xor(){

11

for(c = 1;c < N; c++)

12

cs[c] = (( cs[c] == g[c])?'0':'1');

13

14
15

void crc(){
for(e=0;e<N;e++)

16

cs[e]=t[e];

17
do{

18
19

if(cs[0]=='1')

20

xor();

21

for(c=0;c<N-1;c++)

22

cs[c]=cs[c+1];

23

cs[c]=t[e++];

24

}while(e<=a+N-1);

25

26
27

int main()

28

29

printf("\nEnter data : ");

30

scanf("%s",t);

31

printf("\n----------------------------------------");

32

printf("\nGeneratng polynomial : %s",g);

33

a=strlen(t);

34

for(e=a;e<a+N-1;e++)

t[e]='0';

35
36

printf("\n----------------------------------------");

37

printf("\nModified data is : %s",t);

38

printf("\n----------------------------------------");

39

crc();

40

printf("\nChecksum is : %s",cs);

41

for(e=a;e<a+N-1;e++)
t[e]=cs[e-a];

42
43

printf("\n----------------------------------------");

44

printf("\nFinal codeword is : %s",t);

45

printf("\n----------------------------------------");

46

printf("\nTest error detection 0(yes) 1(no)? : ");

47

scanf("%d",&e);

48

if(e==0)

49

{
do{

50
51

printf("\nEnter the position where error is to be inserted : ");

52

scanf("%d",&e);

53

}while(e==0 || e>a+N-1);

54

t[e-1]=(t[e-1]=='0')?'1':'0';

55

printf("\n----------------------------------------");

56

printf("\nErroneous data : %s\n",t);

57

58

crc();

59

for(e=0;(e<N-1) && (cs[e]!='1');e++);

60

if(e<N-1)

printf("\nError detected\n\n");

61
else

62
63

printf("\nNo error detected\n\n");

64

printf("\n----------------------------------------\n");
return 0;

65
66

Makefile
?
1
2

a.out:crc.c
gcc -ggdb crc.c

PHONY:clean

clean:

rm a.out *~

Example output
Enter data : 1101
---------------------------------------Generatng polynomial : 10001000000100001
---------------------------------------Modified data is : 11010000000000000000
---------------------------------------Checksum is : 1101000110101101
---------------------------------------Final codeword is : 11011101000110101101
---------------------------------------Test error detection 0(yes) 1(no)? : 0
Enter the position where error is to be inserted : 2
---------------------------------------Erroneous data : 10011101000110101101
Error detected

You might also like