-
Notifications
You must be signed in to change notification settings - Fork 77
/
algorithm_vrc.java
95 lines (85 loc) · 2.77 KB
/
algorithm_vrc.java
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
package vrc;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class VRC {
public static void main(String[] args) throws FileNotFoundException, IOException {
File file = new File("input.txt");
if(file.exists()){
FileReader fr= new FileReader(file);
BufferedReader br=new BufferedReader(fr);
String fileContain = br.readLine();
System.out.println("Your file contains: "+ fileContain);
br.close();
String finMes = convMes(fileContain);
System.out.println("Message without parity bit: "+finMes);
String[] tokens=finMes.split(" ");
int cnt=0;
Scanner sc = new Scanner(System.in);
System.out.print("Input 1 for odd parity and 0 for even parity: ");
int urWish = sc.nextInt();
for(int row=0;row<tokens.length;row++){
for(int j=0;j<tokens[row].length();j++){
if(tokens[row].charAt(j)=='1'){
cnt++;
}
}
if(urWish==0){
if(cnt%2!=0){
tokens[row]=tokens[row]+"1";
}
else{
tokens[row] += "0";
}
}
else
if(cnt%2!=0){
tokens[row]+="0";
}
else{
tokens[row] += "1";
}
cnt=0;
}
finMes = String.join(" ", tokens);
System.out.println("Message to be Transmitted: "+ finMes);
FileWriter fw = new FileWriter("vrcOutput.txt");
BufferedWriter bw = new BufferedWriter(fw);
bw.write(finMes);
bw.close();
}
else{
System.out.println("input.txt does not exists");
}
}
public static String convMes(String s){
String mes="";
for(int i=0;i<s.length();i++){
int asciee=(int)s.charAt(i);
mes = mes + binarycon(asciee) + " ";
}
return mes;
}
public static String binarycon(int value){
int a;
String bin="";
String bin1="";
while(value>0){
a=value%2;
bin=bin+""+a;
value/=2;
}
for(int i=bin.length()-1;i>=0;i--){
bin1+=bin.charAt(i);
}
while(bin1.length()<7){
bin1='0'+bin1;
}
return bin1;
}
}