bytestuffing code added

This commit is contained in:
avinal
2021-02-17 23:18:54 +05:30
parent f600bbbdc4
commit 9fe22ce8b6
2 changed files with 116 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
/**
* /mnt/z/my_git/sixth-semester/computer_networks/lab/lab4/BitStuffing.cpp
* @file BitStuffing.cpp
* @author Avinal Kumar
* @since February 17, 2021
*
* Bit Stuffing Implementation in CPP
*/
#include <algorithm>
#include <iostream>
std::string bit_stuff(std::string const data) {
std::string stuffed = "";
size_t last_pos = 0;
while (true) {
size_t one = data.find("11111", last_pos);
if (one != std::string::npos) {
stuffed += data.substr(last_pos, one - last_pos + 5);
stuffed += '0';
last_pos = one + 5;
} else {
break;
}
}
stuffed += data.substr(last_pos);
return stuffed;
}
std::string bit_unstuff(std::string const data) {
std::string unstuff = "";
size_t last_pos = 0;
while (true) {
size_t one = data.find("11111", last_pos);
if (one != std::string::npos) {
unstuff += data.substr(last_pos, one - last_pos + 5);
last_pos = one + 6;
} else {
break;
}
}
unstuff += data.substr(last_pos);
return unstuff;
}
int main(int argc, char const *argv[]) {
std::string data = "";
std::cout << "Enter data stream consisting of 0 and 1 only:" << std::endl;
std::cin >> data;
std::string stuff = bit_stuff(data);
std::cout << "Data after stuffing bits: " << stuff << std::endl;
std::cout << "Data after unstuffing: " << bit_unstuff(stuff) << std::endl;
std::cout << std::endl;
return 0;
}

View File

@@ -0,0 +1,61 @@
/**
* /mnt/z/my_git/sixth-semester/computer_networks/lab/lab4/ByteStuffing.cpp
* @file ByteStuffing.cpp
* @author Avinal Kumar
* @since February 17, 2021
*
* Byte Stuffing Implementation in C++
*/
#include <iostream>
std::string byte_stuff(std::string const data, std::string const escape) {
std::string stuffed = "0x";
size_t last_pos = 2;
while (true) {
size_t esc = data.find(escape, last_pos);
if (esc != std::string::npos) {
stuffed += data.substr(last_pos, esc - last_pos);
stuffed += "5D";
stuffed += escape;
last_pos = esc + 2;
} else {
break;
}
}
stuffed += data.substr(last_pos);
return stuffed;
}
std::string byte_unstuff(std::string const data) {
std::string unstuff = "0x";
size_t last_pos = 2;
while (true) {
size_t esc = data.find("5D", last_pos);
if (esc != std::string::npos) {
unstuff += data.substr(last_pos, esc - last_pos);
unstuff += data.substr(esc + 2, 2);
last_pos = esc + 4;
} else {
break;
}
}
unstuff += data.substr(last_pos);
return unstuff;
}
int main(int argc, char const *argv[]) {
std::string data = "";
std::cout << "Enter hexadecimal data string starting with 0x: " << std::endl;
std::cin >> data;
std::cout << "Flag to escape: ";
std::string escape = "";
std::cin >> escape;
std::string stuff = byte_stuff(data, "5D");
stuff = byte_stuff(stuff, escape);
std::cout << "Data after Byte stuffing: " << stuff << std::endl;
std::string unstuff = byte_unstuff(stuff);
std::cout << "Data after unstuffing: " << unstuff << std::endl;
std::cout << std::endl;
return 0;
}