small code refactor
This commit is contained in:
parent
446d5f2306
commit
744f9793c5
@ -9,11 +9,24 @@ class CrcTask {
|
||||
private String rawMessage;
|
||||
|
||||
CrcTask(String message, String rawMessage) {
|
||||
this.message = formatMessage(message) + "0000000000000000";
|
||||
this.message = formatMessage(toBinary(message)) + "0000000000000000";
|
||||
this.rawMessage = rawMessage;
|
||||
createGeneratingPolynomial();
|
||||
}
|
||||
|
||||
private String toBinary(String s) {
|
||||
byte[] bytes = s.getBytes();
|
||||
StringBuilder binary = new StringBuilder();
|
||||
for (byte b : bytes) {
|
||||
int val = b;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
binary.append((val & 128) == 0 ? 0 : 1);
|
||||
val <<= 1;
|
||||
}
|
||||
}
|
||||
return binary.toString();
|
||||
}
|
||||
|
||||
private String formatMessage(String message) {
|
||||
int firstNonZeroVal = 0;
|
||||
boolean found = false;
|
||||
@ -166,6 +179,7 @@ class CrcTask {
|
||||
}
|
||||
|
||||
boolean decode(String encodedString) {
|
||||
encodedString = toBinary(encodedString);
|
||||
encodedString = fillPolyTo8(encodedString);
|
||||
short[] encodedShortArray = convertBinaryStringToShortArray(encodedString);
|
||||
encodedShortArray = swapPolynomialValues(encodedShortArray);
|
||||
|
@ -3,21 +3,13 @@ package com.tylkowski.crc;
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
CrcTask crcTask = new CrcTask(toBinary(args[0]), args[0]);
|
||||
// ex. in command line type "a"
|
||||
CrcTask crcTask = new CrcTask(args[0], args[0]);
|
||||
System.out.println(crcTask.encode());
|
||||
System.out.println(crcTask.decode(toBinary(crcTask.encode())));
|
||||
String input = crcTask.encode();
|
||||
if (input.length() >= 3) System.out.println(crcTask.decode(input));
|
||||
|
||||
}
|
||||
|
||||
private static String toBinary(String s) {
|
||||
byte[] bytes = s.getBytes();
|
||||
StringBuilder binary = new StringBuilder();
|
||||
for (byte b : bytes) {
|
||||
int val = b;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
binary.append((val & 128) == 0 ? 0 : 1);
|
||||
val <<= 1;
|
||||
}
|
||||
}
|
||||
return binary.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user