Framing
🧩 Syntax:
#include <iostream>
#include <vector>
using namespace std;
void characterCount()
{
string dataStream;
cout << "enter data for character count methode:";
cin >> dataStream;
string ans = "";
int i = 0;
int Count = 1;
while (i < dataStream.length())
{
int FrameLength = int(dataStream[i]) - '0';
if (FrameLength == 0)
{
cout << "frame size cannot be 0.enter correct data" << endl;
break;
}
for (int curr = 0; curr < FrameLength; curr++)
{
if (dataStream[i + curr] != '\0')
{
ans = ans + dataStream[i + curr];
}
}
int len = ans.length();
if (len == FrameLength)
{
cout << "Frame " << Count << " has " << FrameLength << " character and frame is:" << ans << endl;
}
else
{
cout << "Frame " << Count << " has " << FrameLength << " characters but data is unsufficient" << endl;
}
i = i + FrameLength;
Count++;
ans = "";
}
}
void bitStuffing()
{
string bitStream;
cout << "enter binary data for bit stuffing methode:";
cin >> bitStream;
int consecutiveOnes = 0;
string terminal = "01111110";
string frame = terminal;
for (int i = 0; i < bitStream.length(); i++)
{
if ('1' == bitStream[i])
{
consecutiveOnes++;
}
else
{
consecutiveOnes = 0;
}
frame = frame + bitStream[i];
if (consecutiveOnes == 5)
{
consecutiveOnes = 0;
frame = frame + '0';
}
}
frame = frame + terminal;
cout << "Frame which is to be transmitted:" << frame << endl;
}
void characterStuffing()
{
int n;
string byteStream[20];
string data;
vector<string>frame;
cout<<"enter number of bytes:";
cin>>n;
cout<<"data in bytes:"<<endl;
for(int i=0;i<n;i++)
{
cin>>data;
byteStream[i]=data;
}
string start="DLE STX";
string end="DLE ETX";
string escape="DLE";
frame.push_back(start);
for(int i=0;i<n;i++)
{
if(byteStream[i]==escape)
{
frame.push_back(escape);
}
frame.push_back(byteStream[i]);
}
frame.push_back(end);
cout<<"Frame that is to be trasmited is:";
for(int i=0;i<frame.size();i++)
{
cout<<frame[i]<<" ";
}
}
int main()
{
int choice;
cout << "select choice:" << endl;
cout << "1.character count" << endl;
cout << "2.bit stuffing" << endl;
cout << "3.character stuffing" << endl;
cout << "\n\n";
while (1)
{
cout << "enter choice:";
cin >> choice;
switch (choice)
{
case 1:
characterCount();
break;
case 2:
bitStuffing();
break;
case 3:characterStuffing();
break;
default:cout<<"wrong choice";
}
cout<<"\n\n";
}
}