Skip to content

Commit a8e226d

Browse files
authored
Add files via upload
1 parent 616fa0b commit a8e226d

File tree

5 files changed

+472
-0
lines changed

5 files changed

+472
-0
lines changed

Bit_stuffing.java

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

HammingCode.java

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package dcn_lab1;
2+
import java.util.*;
3+
public class HammingCode {
4+
public static void main(String args[])
5+
{
6+
int size, hammingCodeSize, errorPosition,arr[],hammingCode[];
7+
Scanner sc = new Scanner(System.in);
8+
System.out.println("enter data size (in bits)=");
9+
size = sc.nextInt();
10+
arr = new int[size];
11+
for(int j = 0 ; j < size ; j++) { // INPUT DATA
12+
System.out.println("Enter " + (size - j) + "-bit of the data=");
13+
arr[size - j - 1] = sc.nextInt();
14+
}
15+
hammingCode = getHammingCode(arr); // store return value to the hammingCode array
16+
hammingCodeSize = hammingCode.length;
17+
System.out.println("the entered data is=");
18+
for(int i = 0 ; i < hammingCodeSize; i++) {
19+
System.out.print(hammingCode[(hammingCodeSize - i - 1)]);
20+
}
21+
System.out.println("enter the position you want to generate error in(if not 0)= ");
22+
errorPosition = sc.nextInt();
23+
sc.close();
24+
if(errorPosition != 0) {
25+
// alter bit of the user entered position
26+
hammingCode[errorPosition - 1] = (hammingCode[errorPosition - 1] + 1) % 2;
27+
}
28+
System.out.println("the sender data= ");
29+
for(int k = 0; k < hammingCodeSize; k++) {
30+
System.out.print(hammingCode[hammingCodeSize - k - 1]);
31+
}
32+
System.out.println(); // for next line
33+
receiveData(hammingCode, hammingCodeSize - arr.length);
34+
}
35+
static int[] getHammingCode(int data[]) {
36+
int returnData[],size,i = 0, parityBits = 0 ,j = 0, k = 0;
37+
size = data.length;
38+
while(i < size) {
39+
// 2 power of parity bits = current position(number of bits traversed + number of parity bits + 1).
40+
if(Math.pow(2, parityBits) == (i + parityBits + 1)) {
41+
parityBits++;
42+
}
43+
else {
44+
i++;
45+
}
46+
}
47+
returnData = new int[size + parityBits];
48+
// initialize returnData array with '2'
49+
for(i = 1; i <= returnData.length; i++) { // condition to find parity bit location
50+
if(Math.pow(2, j) == i) {
51+
52+
returnData[(i - 1)] = 2;
53+
j++;
54+
}
55+
else {
56+
returnData[(k + j)] = data[k++]; //for odd position
57+
}
58+
}
59+
for(i = 0; i < parityBits; i++) {// use for loop to set even parity bits at parity bit locations
60+
61+
returnData[((int) Math.pow(2, i)) - 1] = getParityBit(returnData, i);
62+
}
63+
64+
return returnData;
65+
}
66+
static int getParityBit(int returnData[], int pow) { //return parity bit based on the power
67+
int parityBit = 0;
68+
int size = returnData.length;
69+
for(int i = 0; i < size; i++) {
70+
if(returnData[i] != 2) {
71+
int k = (i + 1);
72+
String str = Integer.toBinaryString(k);
73+
//Now bit at the 2^(power) location of the binary value of index is 1,
74+
// we check the value stored at that location. If the value is 1 or 0,
75+
// we will calculate the parity value.
76+
int temp = ((Integer.parseInt(str)) / ((int) Math.pow(10, pow))) % 10;
77+
if(temp == 1) {
78+
if(returnData[i] == 1) {
79+
parityBit = (parityBit + 1) % 2;
80+
}
81+
}
82+
}
83+
}
84+
return parityBit;
85+
}
86+
static void receiveData(int data[], int parityBits) { //Detect error
87+
int pow;
88+
int size = data.length;
89+
int parityArray[] = new int[parityBits];
90+
String errorLoc = new String(); // for storing the integer value of the error location
91+
for(pow = 0; pow < parityBits; pow++) {
92+
for(int i = 0; i < size; i++) { //used for 2^power position
93+
int j = i + 1;
94+
String str = Integer.toBinaryString(j);
95+
int bit = ((Integer.parseInt(str)) / ((int) Math.pow(10, pow))) % 10; //find bit
96+
if(bit == 1) {
97+
if(data[i] == 1) {
98+
parityArray[pow] = (parityArray[pow] + 1) % 2;
99+
}
100+
}
101+
}
102+
errorLoc = parityArray[pow] + errorLoc;
103+
}
104+
int finalLoc = Integer.parseInt(errorLoc, 2);
105+
// check whether the finalLoc value is 0 or not
106+
if(finalLoc != 0) {
107+
System.out.println("error at location= " + finalLoc);
108+
data[finalLoc - 1] = (data[finalLoc - 1] + 1) % 2;
109+
System.out.println("After correcting the error, the code is:");
110+
for(int i = 0; i < size; i++) {
111+
System.out.print(data[size - i - 1]);
112+
}
113+
}
114+
else {
115+
System.out.println("no error!!");
116+
}
117+
}
118+
}

dcn_lab2.java

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package dcn_lab1;
2+
import java.util.*;
3+
import java.io.*;
4+
class dcn_lab2
5+
{
6+
public static void main(String args[])throws IOException
7+
{
8+
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
9+
String x="";
10+
dcn_lab2 ob=new dcn_lab2();
11+
System.out.println("Enter binary sequence:");
12+
x=br.readLine();
13+
System.out.println(x);
14+
System.out.println("Choose a polynomial for divisor:");
15+
System.out.println("1.CRC-8\n2.CRC-10\n3.ITU-16\n4.ITU-32\n5.Custom polynomial\n");
16+
int cp=Integer.parseInt(br.readLine());
17+
switch(cp)
18+
{
19+
case 1:ob.findCRC(x,"100000111",8);
20+
break;
21+
case 2:ob.findCRC(x,"11000110101",11);
22+
break;
23+
case 3:ob.findCRC(x,"10001000000100001",16);
24+
break;
25+
case 4:ob.findCRC(x,"10000011100110000010001110110110111",32);
26+
break;
27+
case 5:System.out.println("Enter the degree of polynomial:");
28+
int deg=Integer.parseInt(br.readLine());
29+
StringBuffer polydeg=new StringBuffer(deg+1);
30+
for(int i=0;i<=deg;i++)
31+
polydeg.append('0');
32+
polydeg.setCharAt(deg, '1');
33+
System.out.println("Enter -999 to terminate input of powers for x\nEnter the powers of x in the polynomial:");
34+
int pwrs=Integer.parseInt(br.readLine());
35+
while(pwrs!=-999)
36+
{
37+
pwrs=Integer.parseInt(br.readLine());
38+
}
39+
polydeg.reverse();
40+
String divi=polydeg.toString();
41+
ob.findCRC(x,divi,deg);
42+
}
43+
}
44+
public void findCRC(String seq,String divi,int deg)
45+
{
46+
String crc=seq;
47+
for(int i=0;i<deg;i++)
48+
crc=crc+"0";
49+
int len=divi.length();
50+
String xfind=crc.substring(0,deg+1);
51+
String remain="";
52+
while(len<crc.length())
53+
{
54+
55+
StringBuffer polydeg;
56+
int pwrs=0;
57+
polydeg.setCharAt(pwrs, '1');
58+
if(xfind.charAt(0)=='1')
59+
{
60+
remain=XOR(xfind,divi,deg+1);
61+
xfind=remain+crc.charAt(len);
62+
}
63+
else
64+
xfind=xfind.substring(1)+crc.charAt(len);
65+
len++;
66+
67+
}
68+
System.out.println("CRC:"+seq+xfind.substring(1));
69+
70+
}
71+
public String XOR(String seq,String divi,int len)
72+
{
73+
String remain="";
74+
for(int i=0;i<len;i++)
75+
{
76+
if(seq.charAt(i)==divi.charAt(i))
77+
remain=remain+"0";
78+
else
79+
remain=remain+"1";
80+
}
81+
82+
return remain.substring(1);
83+
}
84+
}

0 commit comments

Comments
 (0)