Structure Query Language, C programming, Java, Servlet, Jsp, Unix

Tuesday 12 June 2012

BIT STUFFING


[ SENDER SIDE ]
#include<stdio.h>
#include<fcntl.h>
#include<string.h>
int getBinary(int num,int pos)
{
int code=1;
code=code<<(pos-1);
if(num & code)
return 1;
else
return 0;
}
void bitStuff(char str[])
{
int i=0,k=0,counter=0;
char temp[50];
strcpy(temp,str);
while(temp[i]!='\0')
{
str[k++]=temp[i];
if(temp[i]=='1')
counter++;
else
counter=0;

if(counter==5)
{
str[k++]='0';
}
i++;
}
str[k]='\0';
}
void main()
{
int pid,i,no,k=0,num;
char str[50];
system(">pipe");
pid=open("pipe",O_WRONLY);
printf("Enter Number to Send");
scanf("%d",&num);
for(i=14;i>0;i--)
{
str[k++]=getBinary(num,i)+48;
}
str[k]='\0';
printf("\nBEFORE STUFF : %s",str);
bitStuff(str);
printf("\nAFTER STUFF  : %s",str);
write(pid,&str,sizeof(str));
}

[ RECEIVER SIDE ]

#include<stdio.h>
#include<fcntl.h>
#include<string.h>
void deStuff(char str[])
{
int i=0,counter=0,k=0;
char temp[50];
strcpy(temp,str);
while(temp[i]!='\0')
{
str[k++]=temp[i];
if(temp[i]=='1')
counter++;
else
counter=0;
if(counter==5)
i++;
i++;
}
str[k]='\0';
}
void main()
{
int pid,i=0;
char str[50],ans[50];
pid=open("pipe",O_RDONLY);
read(pid,&str,sizeof(str));
printf("\nBEFORE DESTUFF : %s",str);
deStuff(str);
printf("\nAFTER DESTUFF : %s",str);
}

1 comment: