-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoder.java
37 lines (32 loc) · 1.16 KB
/
Coder.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
import java.sql.Time;
import java.util.concurrent.TimeUnit;
public class Coder {
public static String start(String message) {
StringBuilder result = new StringBuilder();
for (char c : message.toCharArray()) {
StringBuilder binaryCharWithEmptyParityBits = convertToBinaryWithParityBits(c);
StringBuilder binaryCharWithCalculatedParityBits = Helpers.replaceParityBits(binaryCharWithEmptyParityBits);
result.append(binaryCharWithCalculatedParityBits);
}
return result.toString();
}
/**
* Converts single symbol to a binary String with 4 parity bits
*
* @param symbol char to convert
* @return StringBuilder with 12 bits
*/
public static StringBuilder convertToBinaryWithParityBits(char symbol) {
StringBuilder binaryString = new StringBuilder();
int val = symbol;
for (int i = 0; i < 12; i++) {
if (Helpers.checkPosition(i)) {
binaryString.append(0);
} else {
binaryString.append((val & 128) == 0 ? 0 : 1);
val <<= 1;
}
}
return binaryString;
}
}