Bit Stuffing Code Implementation in Java

programming

CRC16 using JAVAThis is an implementation of the Bit Stuffing popularly used in data communication, also known as one of the concepts of Framing data bits. Bit stuffingis the process of inserting noninformation bits into data to break up bit patterns to affect the synchronous transmission of information. It is widely used in network and communication protocols, in which bit stuffing is a required part of the transmission process.To know more please click here. 

 

Sample Output:

Bit Stuffing

This is a sample output with data binary data entered as 11001111110.

Code:
import java.util.*;
public class BitStuffing {
 public static void main(String[] args) {
  System.out.print("Enter the Binary message: ");
  Scanner sn = new Scanner(System.in);
  String data = sn.nextLine();
  String res = new String();
  String out = new String();
  int counter = 0;
  for (int i = 0; i & lt; data.length(); i++) {
   if (data.charAt(i) != '1' & amp; & amp; data.charAt(i) != '0') {
    System.out.println("Enter only Binary values!!!");
    return;
   }
   if (data.charAt(i) == '1') {
    counter++;
    res = res + data.charAt(i);
   } else {
    res = res + data.charAt(i);
    counter = 0;
   }
   if (counter == 5) {
    res = res + '0';
    counter = 0;
   }
  }
  String inc = "01111110" + res + "01111110";
  System.out.println("The Message to be transfered: " + inc);
  System.out.println("Sending Message....");
  counter = 0;
  for (int i = 0; i & lt; res.length(); i++) { < /pre>
   if (res.charAt(i) == '1') {

    counter++;
    out = out + res.charAt(i);

   } else {
    out = out + res.charAt(i);
    counter = 0;
   }
   if (counter == 5) {
    if ((i + 2) != res.length())
     out = out + res.charAt(i + 2);
    else
     out = out + '1';
    i = i + 2;
    counter = 1;
   }
  }

  System.out.println("Message Received...Successfully!!!");
  System.out.println("The Destuffed Message is: " + out);
 }
}
Description:

Data Entered is: 11001111110
Flag: 01111110
So basically we look for six consecutive 1’s in our data and stuff or insert a “0” after five 1’s to distinguish the data from the flag. While transmitting we insert the flag at both starting and beginning. At the Receiver’s side, we have to do stuff our encoded message. Hence we discard the flag and the stuffed “0” to get our original data.

Catch me

Sidharth Patnaik

Blogger at Codermag
Tinkerer/Coder/Blogger
Catch me

Latest posts by Sidharth Patnaik (see all)

Related posts