-
Notifications
You must be signed in to change notification settings - Fork 0
/
encodeCustom.java
96 lines (81 loc) · 2.26 KB
/
encodeCustom.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
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 com.sun.java_cup.internal.runtime.Scanner;
public class encodeCustom
{
public static void main(String[] args) throws IOException
{
encodeCustom encoder = new encodeCustom();
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(args[0])));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(args[1])));
int c;
while((c = in.read()) != -1)
{
out.write(encoder.CharToPN((char)c));
out.write("\n");
}
out.close();
}
private String CharToPN(char c)
{
// binary format of ascii of char c
String binaryStringShort = Integer.toBinaryString((int)c);
// fill the 0s to get 8 bits
StringBuffer paddingBuffer = new StringBuffer();
while(paddingBuffer.length() < 8 - binaryStringShort.length())
{
paddingBuffer.append("0");
}
String binaryString = paddingBuffer.toString() + binaryStringShort;
// split into 2 parts and encode each one
String[] binaryStrings = new String[] {
binaryString.substring(0, 4),
binaryString.substring(4, 8)
};
StringBuffer codewordBuffer = new StringBuffer();
for (String bits : binaryStrings)
{
// if count #one is odd, check is the same as info, or complement
String check;
if(CountOne(bits) % 2 == 1)
{
check = bits;
}
else
{
check = Complement(bits);
}
// combine info with check
codewordBuffer.append(bits + check);
}
return codewordBuffer.toString();
}
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();
}
}