-
Notifications
You must be signed in to change notification settings - Fork 0
/
decodeCombined.java
196 lines (169 loc) · 4.42 KB
/
decodeCombined.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import com.sun.java_cup.internal.runtime.Scanner;
public class decodeCombined
{
public static void main(String[] args) throws IOException
{
decodeCombined decoder = new decodeCombined();
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(args[0])));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(args[1])));
String recieved;
while((recieved = in.readLine()) != null)
{
int ascii = decoder.CombinedToAscii(recieved);
if(ascii < 128)
{
out.write((char)ascii);
}
}
out.close();
}
private int CombinedToAscii(String recieved)
{
return PNToAscii(CombinedToPN(recieved));
}
private String CombinedToPN(String recieved)
{
// split the recieved code into 2 parts
String[] recieveds = new String[] {
recieved.substring(0, 7),
recieved.substring(7, 14),
recieved.substring(14, 21),
recieved.substring(21, 28)
};
// decode each part and join them to get the result byte
StringBuffer decoded = new StringBuffer();
for (String recievedi : recieveds) {
int indexSyndrome = getSyndromeIndex(recievedi);
int flipPosition = syndromeTable[indexSyndrome];
// flip a bit if needed
StringBuilder stringBuilder = new StringBuilder(recievedi);
if(flipPosition != -1)
{
char flip = recievedi.charAt(flipPosition) == '0' ? '1' : '0';
stringBuilder.setCharAt(flipPosition, flip);
}
// remove the partial check bits
decoded.append(stringBuilder.toString().substring(0, 4));
}
return decoded.toString();
}
private int PNToAscii(String recieved)
{
// split the recieved code into 2 parts
String[] recieveds = new String[] {
recieved.substring(0, 8),
recieved.substring(8, 16)
};
// decode each part and join them to get the result byte
StringBuffer decoded = new StringBuffer();
for (String recievedi : recieveds) {
String info = recievedi.substring(0, 4);
String check = recievedi.substring(4, 8);
String test;
// obtain test according to #one of info
if(CountOne(info) % 2 == 1)
{
test = AddByBit(info, check);
}
else
{
test = Complement(AddByBit(info, check));
}
// choose to trust info or check according to #one of test
if(CountOne(test) < 2)
{
decoded.append(info);
}
else
{
if(CountOne(info) % 2 == 1)
{
decoded.append(Complement(check));
}
else
{
decoded.append(check);
}
}
}
int ascii = Integer.parseInt(decoded.toString(), 2);
return ascii;
}
private int CountOne(String binaryString)
{
int countOne = 0;
for(int i = 0; i < binaryString.length(); ++i)
{
if(binaryString.charAt(i) == '1')
countOne++;
}
return countOne;
}
private String Complement(String binaryString)
{
StringBuilder complementBuilder = new StringBuilder(binaryString);
for(int i = 0; i < binaryString.length(); ++i)
{
char flip = binaryString.charAt(i) == '0' ? '1' : '0';
complementBuilder.setCharAt(i, flip);
}
return complementBuilder.toString();
}
private String AddByBit(String str1, String str2)
{
StringBuffer sumBuffer = new StringBuffer();
for(int i = 0; i < str1.length(); ++i)
{
if(str1.charAt(i) == str2.charAt(i))
{
sumBuffer.append("0");
}
else
{
sumBuffer.append("1");
}
}
return sumBuffer.toString();
}
private int getSyndromeIndex(String recievedi) {
int[] recieved = new int[7];
for(int i = 0; i < 7; ++i)
{
recieved[i] = recievedi.charAt(i) == '0' ? 0 : 1;
}
int[] syndrome = new int[3];
Arrays.fill(syndrome, 0);
// calc syndrome by z=Hr
for(int i = 0; i < 3; ++i)
for(int k = 0; k < 7; ++k)
{
syndrome[i] += H[i][k] * recieved[k];
}
// convert to index
int indexSyndrome = 0;
for (int i = 0 ; i < 3; ++i) {
syndrome[i] %= 2;
indexSyndrome += syndrome[i] * (int)Math.pow((double)2, (double)(2 - i));
}
return indexSyndrome;
}
private static int[][] H = new int[][] {
{1, 1, 1, 0, 1, 0, 0},
{0, 1, 1, 1, 0, 1, 0},
{1, 0, 1, 1, 0, 0, 1}
};
private static int[] syndromeTable = new int[] {
-1, 6, 5, 3, 4, 0, 1, 2
};
}