0% found this document useful (0 votes)
8 views5 pages

Lab Codes

Uploaded by

drkalpanacse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Lab Codes

Uploaded by

drkalpanacse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

===========================================================

LAB EXPERIMENT: BIT STUFFING & CHARACTER STUFFING


===========================================================

 WHAT IS BIT STUFFING?


Bit stuffing is a technique used in data transmission where the sender adds extra 0s into the data
stream to prevent the occurrence of flag patterns (like 01111110) within the data.

 WHY STUFF A 0 AFTER FIVE 1s?


In protocols like HDLC, the frame starts and ends with the flag `01111110` (hex: 0x7E).
If the data has 5 consecutive 1s, a 0 is inserted automatically to prevent confusion with the flag.

Example:
Input : 11111010 → Stuffed: 111110010 (0 added after five 1s)

👉 WHAT IS CHARACTER STUFFING?


Character stuffing is used for text data. Frames begin with a special sequence and end with
another.

✔ Start of Frame: DLE + STX


✔ End of Frame : DLE + ETX
✔ If DLE , STX , or ETX appear in data, an extra DLE is inserted before it (escaping).
// BIT STUFFING CODE (Without Flags)
// ==============================
#include <stdio.h>
#include <string.h>

int main() {
int data[100], stuffed[200];
int n, i, j = 0, count = 0;

printf("Enter frame length: ");


scanf("%d", &n);

printf("Enter the input bits (0s and 1s only):\n");


for (i = 0; i < n; i++) {
scanf("%d", &data[i]);
}

// Bit stuffing logic


for (i = 0; i < n; i++) {
stuffed[j++] = data[i];
if (data[i] == 1) {
count++;
if (count == 5) {
stuffed[j++] = 0; // Stuff a 0 after five consecutive 1s
count = 0;
}
} else {
count = 0;
}
}

// Print only stuffed data (without flags)


printf("\nAfter stuffing, the frame is: ");
for (i = 0; i < j; i++) {
printf("%d", stuffed[i]);
}
printf("\n");

return 0;
}

// ==============================
// CHARACTER STUFFING - ESCAPE BASED
// ==============================
/*
This method starts the frame with DLE STX (ASCII 16, 2)
and ends with DLE ETX (ASCII 16, 3).
If any of the control characters (DLE, STX, ETX) appear
in the data, we insert another DLE before it (escape).
*/
#include <stdio.h>
#include <string.h>

int main() {
char input[100], stuffed[200];
int i = 0, j = 0;

printf("Enter the data string: ");


scanf("%s", input);

// Add starting DLE STX


stuffed[j++] = 16; // DLE
stuffed[j++] = 2; // STX

while (input[i] != '\0') {


if (input[i] == 16 || input[i] == 2 || input[i] == 3) {
stuffed[j++] = 16; // Add DLE before special char
}
stuffed[j++] = input[i++];
}

// Add ending DLE ETX


stuffed[j++] = 16; // DLE
stuffed[j++] = 3; // ETX

// Print ASCII values


printf("\nStuffed Frame (ASCII Values):\n");
for (i = 0; i < j; i++) {
printf("%d ", (unsigned char)stuffed[i]);
}
printf("\n");

return 0;
}

// ==============================
// ✅ CHARACTER STUFFING - POSITION BASED
// ==============================
/*
In this version, the user selects a position in the string
where they want to insert a character with escape (DLE) on both sides.
The whole frame is wrapped with "dlestx" and "dleetx".
*/
#include <stdio.h>
#include <string.h>

int main() {
char input[100], stuffed[200];
int i = 0, j = 0, pos;
char ch;

printf("Enter data string: ");


scanf("%s", input);

printf("Enter position to insert character (1-based index): ");


scanf("%d", &pos);

if (pos < 1 || pos > strlen(input)) {


printf("Invalid position\n");
return 0;
}

printf("Enter the character to insert: ");


scanf(" %c", &ch);
strcpy(stuffed, "dlestx");
j = strlen(stuffed);

while (input[i] != '\0') {


if (i == pos - 1) {
stuffed[j++] = 'd';
stuffed[j++] = 'l';
stuffed[j++] = 'e';
stuffed[j++] = ch;
stuffed[j++] = 'd';
stuffed[j++] = 'l';
stuffed[j++] = 'e';
}
stuffed[j++] = input[i++];
}

strcat(stuffed, "dleetx");

printf("\nStuffed Frame: %s\n", stuffed);

return 0;}

You might also like