Bit Stuffing Program
Bit Stuffing Program
#include
#include
main()
{
char a[20],fs[50]="",t[6],r[5];
int i,j,p=0,q=0;
clrscr();
printf("enter bit string : ");
scanf("%s",a);
strcat(fs,"01111110");
if(strlen(a)<5)
{
strcat(fs,a);
}
else
{
for(i=0;i<STRLEN(A)-4;I++)
{
for(j=i;j<I+5;J++)
{
t[p++]=a[j];
}
t[p]='\0';
if(strcmp(t,"11111")==0)
{
strcat(fs,"111110");
i=j-1;
}
else
{
r[0]=a[i];
r[1]='\0';
strcat(fs,r);
}
p=0;
}
for(q=i;q<STRLEN(A);Q++)
{
t[p++]=a[q];
}
t[p]='\0';
strcat(fs,t);
}
strcat(fs,"01111110");
printf("After stuffing : %s",fs);
getch();
}
#include
#include
main()
{
char a[30],fs[50]="",t[3],sd,ed,x[3],s[3],d[3],y[3];
int i,j,p=0,q=0;
clrscr();
printf("Enter characters to be stuffed : ");
scanf("%s",a);
printf("\nEnter a character that represents starting delimiter : ");
scanf(" %c",&sd);
printf("\nEnter a character that represents ending delimiter : ");
scanf(" %c",&ed);
x[0]=s[0]=s[1]=sd;
x[1]=s[2]='\0';
y[0]=d[0]=d[1]=ed;
d[2]=y[1]='\0';
strcat(fs,x);
for(i=0;i<STRLEN(A);I++)
{
t[0]=a[i];
t[1]='\0';
if(t[0]==sd)
strcat(fs,s);
else
if(t[0]==ed)
strcat(fs,d);
else
strcat(fs,t);
}
strcat(fs,y);
printf("\nAfter stuffing : %s",fs);
getch();
}
#include< stdlib.h>
#include< conio.h>
#include< stdio.h>
void main( )
{
int i,j,n,g,a,arr[20],gen[20],b[20],q[20],s;
clrscr( );
printf("Transmitter side:");
printf("\nEnter no. of data bits:");
scanf("%d",&n);
printf("Enter data:");
for(i=0;i< n;i++)
scanf("%d",&arr[i]);
printf("Enter size of generator:");
scanf("%d",&g);
do{
printf("Enter generator:");
for(j=0;j< g;j++)
scanf("%d",&gen[j]);
}
while(gen[0]!=1);
printf("\n\tThe generator matrix:");
for(j=0;j< g;j++)
printf("%d",gen[j]);
a=n+(g-1);
printf("\n\tThe appended matrix is:");
for(i=0;i< j;++i)
arr[n+i]=0;
for(i=0;i< a;++i)
printf("%d",arr[i]);
for(i=0;i< n;++i)
q[i]= arr[i];
for(i=0;i< n;++i)
{
if(arr[i]==0)
{
for(j=i;j< g+i;++j)
arr[j] = arr[j]^0;
}
else
{
arr[i] = arr[i]^gen[0];
arr[i+1]=arr[i+1]^gen[1];
arr[i+2]=arr[i+2]^gen[2];
arr[i+3]=arr[i+3]^gen[3];
}
}
printf("\n\tThe CRC is :");
for(i=n;i < a;++i)
printf("%d",arr[i]);
s=n+a;
for(i=n;i< s;i++)
q[i]=arr[i];
printf("\n");
for(i=0;i< a;i++)
printf("%d",q[i]);
getch( );
}
Cyclic Redundancy Check Output
Transmitter side:
Enter no. of data bits:8
Enter data:1 0 1 0 0 0 0 1
Enter size of generator:4
Enter generator:1 0 0 1
#include <codecogs/io/compression/lzw.h>
#include <iostream>
// the number of characters to generate in the sample array
#define N 10000
using namespace IO::Compression;
int main()
{
// initialize random seed
srand(time(0));
// generate an array of N random letters
std::vector<unsigned char> sample;
for (int i = 0; i < N; ++i)
sample.push_back('A' + rand() % ('Z' - 'A' + 1));
// compress the sample array
std::vector<unsigned char> compressed = LZW::compress(sample);
// decompress the compressed array
std::vector<unsigned char> uncompressed = LZW::decompress(compressed);
// compare the sizes of the compressed and uncompressed arrays
std::cout << " Size of the sample array: " << N << std::endl;
std::cout << " Size of the compressed array: " << compressed.size() << std::endl;
std::cout << "Size of the uncompressed array: " << uncompressed.size() << std::endl;
std::cout << std::endl;
// test if the sample and the uncompressed arrays are identical
// this proves that the LZW compression algorithm does not affect the initial data
bool identical = (N == uncompressed.size());
for (size_t i = 0; identical && i < uncompressed.size(); ++i)
if (sample[i] != uncompressed[i])
identical = false;
if (identical)
std::cout << "The sample and uncompressed arrays are identical." << std::endl;
else
std::cout << "Error! The sample and uncompressed arrays are NOT identical." << std::endl;
return 0;
}
Output of LZW
Size of the sample array: 10000
Size of the compressed array: 7621
Size of the uncompressed array: 10000
The sample and uncompressed arrays are identical.
/*************************************************Hamming Code***************************************************/
#include<stdio.h>
#include<math.h>
#include<string.h>
void summ(int i)
{
/* The method to initialize contents of global array by power of 2's sum for each entry */
int j=0; //index no
if(isPow2(i))
{
/*input no i is of power of 2's form like 1,2,4,8,16...*/
arr[ele][ind]=calcPow(i);
/*calculate what's the power of 2 , in the no, & store to global array*/
ind++;
}
else
{
/*input no i is NOT of power of 2's form */
j=findLowerOf2(i);
/* try to find just last no , which is of form of power of 2 */
arr[ele][ind]=calcPow(j);
/* again calculate the power*/
ind++;
i=i-j; /* differnce in the no & the last no which is of form of power of 2 */
/*now call method recursively for the new no (i=i-j) */
summ(i);
}
}
int isPow2(int i)
{
/*if input no is power of two retrun 1 , else 0*/
if(MAXINT % i==0)
return 1; //true
return 0; //false
}
int calcPow(int i)
{
/*Thism ethod returns , what is power of 2 , in a no.
which is of form 2 to the power p */
/* return p , from input of format 2^p */
int count=-1;
/* validate */
if(!isPow2(i))
printf("flow error...
");
else
while(i>0)
{
i/=2;
count++;
}
return count;
}
int findLowerOf2(int i)
{
/*a function to calculate the no , JUST below i , which is power of 2 */
int count=-1;
if(isPow2(i))
return pow(2,i);
else
while(i>0)
{
i/=2;
count++;
}
return pow(2,count);
}
void callSumm(int i)
{
/* A method to call summ() method , with assertion that
all global parameters are incremented at each call to summ()
*/
ind=0;
summ(i);
arr[ele][ind++]=-1;
ele++;
}
void dieError()
{
/* If failure , exit the program*/
exit(1);
}
int calr(int m)
{
/*Method to calculate checksum bits for
given m , databits
*/
int r=0;
for(r=0;r<=m;r++)
if(m <= pow(2,r)-1-r)
return r;
}
int isEven(int i)
{
return i%2==0;
}
/* validate input */
checkInput(argc,argv[1]);
for(j=0,k=0;j<r;j++)
{
for(i=0,k=0;i<len;i++)
if(howManyTimes(j,arr[i]))
count[j][k++]=i+1;
count[j][k]=-1;
}
/* Now **********
Frame all equations & solve them
& fill entries in coded table accordingly
*/
for(i=0;i<r;i++)
{
for(flag=0,j=1;count[i][j]!=-1;j++)
{
temp=count[i][j]-1;
if (coded[temp]=='1')
flag+=1;
else if (coded[temp]=='0')
flag+=0;
}
temp=count[i][0]-1;
if (isEven(flag))
coded[temp]='0';
else
coded[temp]='1';
}
int sd;
struct sockaddr_in serverSock;
char buf[LINE_ARRAY_SIZE];
int min(int arg1, int arg2);
void setup();
void * sendText(void* arg);
void * recvText(void* arg);
int main(){
pthread_t sendTr, recvTr;
struct sockaddr_in serverAddress;
int socketDescriptor;
char c;
int len;
setup();
cout << "Enter the host's IP address: ";
cin.get(buf, MAX_LINE, '\n');
memset(&serverAddress, 0, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(SERVER_PORT);
inet_pton(AF_INET, buf, &serverAddress.sin_addr);
socketDescriptor = socket(AF_INET, SOCK_DGRAM, 0);
}
return 0;
}
int min(int arg1, int arg2){
if(arg1 arg2)
{
return arg2;
}
else
{
return arg1;
}
}
void setup(){
memset(&serverSock, 0, sizeof(serverSock));
serverSock.sin_family = AF_INET;
serverSock.sin_addr.s_addr = htonl(INADDR_ANY);
serverSock.sin_port = htons(SERVER_PORT);
}
void * sendText(void* arg){
char c;
cout << "thread started..\n";
cout << "Input: ";
cin.get(buf, MAX_LINE, '\n');
while (cin.get(c) && c != '\n')
;
cout << "thread finished...\n";
}