-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBit_stuffing.java
More file actions
110 lines (92 loc) · 2.93 KB
/
Bit_stuffing.java
File metadata and controls
110 lines (92 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package dcn_lab1;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.StringBuilder;
import java.io.*;
import java.util.*;
public class Bit_stuffing {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.println("YOUR FILE NAME~ File.txt ");
//File Creation
System.out.println("ENTER NAME OF YOUR FILE~ ");
String filename=sc.nextLine();
File name = new File(filename);
boolean value = name.createNewFile();
if(value) {
System.out.println("FILE SUCCESSFULLY CREATED!");
}
else {
System.out.println("FILE ALREADY EXIST!!");
}
//entering some data
System.out.println("ENTER DATA IN FILE~ ");
String obj=sc.nextLine();
Scanner sc1 = new Scanner(obj);
while (sc1.hasNextLine()) {
String sc2 = sc1.nextLine();
StringBuilder str = new StringBuilder();
for (char ch:sc2.toCharArray()) {
int temp=ch;
str.append(Integer.toBinaryString(temp));
}
System.out.println("YOUR DATA IN BITS LOOKS LIKE~ ");
System.out.println(str);
//BIT STUFFING
int count =0;
StringBuilder str1 = new StringBuilder();
for (int i=0;i< str.length();i++)
{
if (str.charAt(i)=='1')
{
count++;
str1.append(str.charAt(i));
if (count == 5)
{
str1.append("0");
count =0;
}
}
else
{
str1.append(str.charAt(i));
count =0;
}
}
System.out.println("AFTER BITSTUFFING~ ");
System.out.println(str1);
//DE-STUFFING
StringBuilder d = new StringBuilder();
for (int i=0;i< str1.length();i++)
{
if (str1.charAt(i)=='1')
{
count++;
d.append(str1.charAt(i));
if (count == 5)
{
count =0;
i++;
continue;
}
}
else
{
d.append(str1.charAt(i));
count =0;
}
}
System.out.println("DESTUFFING COMPLETED");
System.out.println(d);
// comparison
if((d.toString().equals(str.toString())))
{
System.out.println("THE DATA SEND WAS CORRECT!!");
}
else {
System.out.println("SOMETHING WENT WRONG TRY AGAIN!!");
}
}
sc1.close();
}
}