Showing posts with label Cyclic Redundancy Check method. Show all posts
Showing posts with label Cyclic Redundancy Check method. Show all posts

Wednesday, April 13, 2016

W.A.P to implement Cyclic Redundancy Check method.



#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define DIVLENGTH 4
#define DIV 1101

int rmainB (long int x,int y);
char *binadd(int,int);
char *dec2bin(int rem);
int mPow(int x,int y);
int bin2dec(long int x);

void main()
{
char *str,*crc,*tmp;
int i=0,n;
clrscr();
printf("\n\n\t Enter Info: ");
scanf("%s",str);
while(str[i]!='\0')
i++;
for(n=0;n<DIVLENGTH-1;n++)
str[i+n]='0';
str[i+n]='\0';
crc=dec2bin(rmainB(atol(str),DIV));
n=strlen(crc);
str=binadd(bin2dec(atol(str)),bin2dec(atol(crc)));
printf("\n\n\t Sender Send. \n\n\t Data : %s\n\n",str);
printf("\n\n\t Divisior : %d ", DIV);
printf("\n\n\t Waiting For Receiver's Response..");
for(i=0;i<6;i++)
{
printf(".");
delay(1000);
}
if(rmainB(atol(str),DIV)==0)
printf("\n\tAccepted..");
else
printf("\n\tRejected..");
printf("\n\n\tPress Any Key To Continue...");
getch();
}

int mPow(int x,int y)
{
int ret=1,i;
for(i=1;i<=y;i++)
ret*=x;
return ret;
}

int bin2dec(long int x)
{
int i=0,bx=0;
while(x > 0)
{
bx+=mPow(2,1)*(x % 10);
i++;
x=x/10;
}
return bx;
}

char *dec2bin(int rem)
{
int i=0;
char *tmp;
while(rem > 0)
{
if(rem % 2 == 0)
tmp[i]='0';
else
tmp[i]='1';
i++;
rem=rem/2;
}
if(i==0)
{
tmp[i++]='0';
tmp[i]='\0';
}
else
{
tmp[i]='\0';
tmp=strrev(tmp);
}
return tmp;
}

char *binadd(int x, int y)
{
char *tmp;
x=x+y;
tmp=dec2bin(x);
return tmp;
}

int rmainB(long int x, int y)
{
int rem,bx=0,by=0;
bx=bin2dec(x);
by=bin2dec(y);
rem= bx % by;
return rem;
}