-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlrc.java
111 lines (100 loc) · 3.18 KB
/
lrc.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package lrc;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class LRC {
public static void main(String[] args) throws IOException {
File file = new File("input.txt");
if(file.exists()){
FileReader fr= new FileReader(file);
String fileContain;
try (BufferedReader br = new BufferedReader(fr)) {
fileContain = br.readLine();
System.out.println("Your file contains: "+ fileContain);
br.close();
}
fr.close();
String finMes = convMes(fileContain);
System.out.println("Message without parity bit: "+finMes);
System.out.println("");
String[] tokens=finMes.split(" ");
String lrc = "";
int col,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();
if(urWish==0){
for(col=0;col<7;col++){
for (String token : tokens) {
if (token.charAt(col) == '1') {
cnt++;
}
}
if(cnt%2==0){
lrc+="0";
}
else{
lrc+="1";
}
cnt=0;
}
}
else{
for(col=0;col<7;col++){
for (String token : tokens) {
if (token.charAt(col) == '1') {
cnt++;
}
}
if(cnt%2==0){
lrc+="1";
}
else{
lrc+="0";
}
cnt=0;
}
}
finMes = String.join(" ", tokens);
// System.out.println("fin: "+finMes);
finMes =finMes + " " + lrc;
System.out.println("Message to be transmitted: "+ finMes);
FileWriter fw = new FileWriter("lrcOutput.txt",false);
//BufferedWriter br =new BufferedWriter(fw);
fw.write(finMes);
fw.close();
}
else{
System.out.println("File doesn't exist");
}
}
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;
}
}