0% found this document useful (0 votes)
89 views

Bit Stuffing Program

The document contains code snippets and output related to different data transmission techniques including bit stuffing, character stuffing, cyclic redundancy check (CRC), Lempel–Ziv–Welch (LZW) compression, and Hamming codes. The bit stuffing and character stuffing code samples take in input strings and output the stuffed strings. The CRC code calculates the CRC from input data and generator bits. The LZW compression code compresses and decompresses a random sample array and compares the sizes. The Hamming code initializes a global array using power of 2 sums.

Uploaded by

Atul Kandwal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views

Bit Stuffing Program

The document contains code snippets and output related to different data transmission techniques including bit stuffing, character stuffing, cyclic redundancy check (CRC), Lempel–Ziv–Welch (LZW) compression, and Hamming codes. The bit stuffing and character stuffing code samples take in input strings and output the stuffed strings. The CRC code calculates the CRC from input data and generator bits. The LZW compression code compresses and decompresses a random sample array and compares the sizes. The Hamming code initializes a global array using power of 2 sums.

Uploaded by

Atul Kandwal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 18

/************************** 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();
}

BIT STUFFING OUTPUT

Enter bit string : 10101111110

After stuffing : 0111111010101111101001111110

Enter bit string : 1011111011110111110

After stuffing : 0111111010111110011110111110001111110


/******************** CHARACTER STUFFING PROGRAM********************/

#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();
}

CHARACTER STUFFING OUTPUT

Enter characters to be stuffed : goodday

Enter a character that represents starting delimiter : d

Enter a character that represents ending delimiter : g

After stuffing : dggooddddayg


/*********************Calculation of CRC (Cyclic Redundancy Check)***********************/

#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

The generator matrix:1001


The appended matrix is:10100001000
The CRC is :111
10100001111

/*********************************************LZW compression Algorithm*****************************************/

#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>

#define MAXINT 65536


#define SIZE 100

int arr[SIZE][SIZE]; // a global array , to contain table information


int ele=0,ind=0; // indices to be used in above array

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 howManyTimes(int val,int a[])


{
/* a method to check that how many times
no val is occuring in array a[]
*/
int i,count=0;
for(i=0;a[i]!=-1;i++)
if(a[i]==val)
count++;
return count;
}

void checkInput(int argc,char str[])


{
int i=0;
if (argc<2)
{
printf("usage: filename.o 'The code string' ");
printf("
ex.
a.out 110110
");
dieError();
}
for(i=0;i<strlen(str);i++)
if(!(str[i]=='0' || str[i]=='1'))
{
printf("Please enter a binary string only.....
");
dieError();
}
}

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;
}

int main(int argc,char *argv[])


{
/* Declaretions ...*/
/* flag & index variables*/
int i,j,k=0,flag,temp;
/* The output codeword container */
char coded[SIZE];
int len; //total length of coded word
int m; //code bits
int r; //check bits
/* to associate & contain equations of checkbits */
int count[SIZE][SIZE];

/* validate input */
checkInput(argc,argv[1]);

/*calculate no of check bits required n thus total length */


m=strlen(argv[1]);
r=calr(m);
len=m+r;

/* Fill the global container , according to the size info


of m,r & len */
for(i=1;i<=len;i++)
callSumm(i);

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;
}

/*Fill the code word....,except check bits*/


for(i=0,j=0;j<len;j++)
{
if(!isPow2(j+1))
coded[j]=argv[1][i++];
else
coded[j]='x'; //initialize checkbits by character x

/* 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';
}

printf("The Hamming coded word for your data is


");
for(i=0;i<len;i++)
printf("%c",coded[i]);
printf("
");
}
/************************Socket Programming in C to implement a Listener and a Talker***********************/
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <iostream>
#include <pthread.h>

#define MAX_LINE 256


#define LINE_ARRAY_SIZE (MAX_LINE+1)
#define SERVER_PORT 15001

using namespace std;

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);

pthread_create( &sendTr, NULL, sendText, NULL );


//pthread_create( &recvTr, NULL, recvText, NULL );

cout << "before while\n";


while (strcmp(buf, ".")) {
// Send the line to the server.

cout << "in while0... \n";

if( len 0 && buf[len-1]=='\n'){


buf[len-1] = 0; /* strip the newline from the end */
len = min(strlen(buf), MAX_LINE); /* limit the size */

sendto(socketDescriptor, buf, len, 0, (struct


sockaddr*)&serverAddress, sizeof(serverAddress));
cout << "in while1... \n";
memset(buf, 0x0, LINE_ARRAY_SIZE);
cout << "in while2... \n";
// Read the modified line back from the server.
if (recv(sd, buf, MAX_LINE, 0) < 0) {
cerr << "didn't get response from server?";
close(sd);
exit(1);
}
}
cout << "Recieved: " << buf << "\n";
memset(buf, 0x0, LINE_ARRAY_SIZE);

}
return 0;
}
int min(int arg1, int arg2){
if(arg1 arg2)
{
return arg2;
}
else
{
return arg1;
}
}
void setup(){

sd = socket(AF_INET, SOCK_DGRAM, 0);

memset(&serverSock, 0, sizeof(serverSock));
serverSock.sin_family = AF_INET;
serverSock.sin_addr.s_addr = htonl(INADDR_ANY);
serverSock.sin_port = htons(SERVER_PORT);

if( bind(sd, (struct sockaddr *) &serverSock, sizeof(serverSock)) )


puts( "bind() failed" );

}
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";
}

You might also like