Zadananie 3 - CRC #26

Closed
s426271 wants to merge 8 commits from s426271/DALGLI0:zadanie-3 into master
2 changed files with 21 additions and 15 deletions
Showing only changes of commit 744f9793c5 - Show all commits

View File

@ -9,11 +9,24 @@ class CrcTask {
private String rawMessage; private String rawMessage;
CrcTask(String message, String rawMessage) { CrcTask(String message, String rawMessage) {
this.message = formatMessage(message) + "0000000000000000"; this.message = formatMessage(toBinary(message)) + "0000000000000000";
this.rawMessage = rawMessage; this.rawMessage = rawMessage;
createGeneratingPolynomial(); 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) { private String formatMessage(String message) {
int firstNonZeroVal = 0; int firstNonZeroVal = 0;
boolean found = false; boolean found = false;
@ -166,6 +179,7 @@ class CrcTask {
} }
boolean decode(String encodedString) { boolean decode(String encodedString) {
encodedString = toBinary(encodedString);
encodedString = fillPolyTo8(encodedString); encodedString = fillPolyTo8(encodedString);
short[] encodedShortArray = convertBinaryStringToShortArray(encodedString); short[] encodedShortArray = convertBinaryStringToShortArray(encodedString);
encodedShortArray = swapPolynomialValues(encodedShortArray); encodedShortArray = swapPolynomialValues(encodedShortArray);

View File

@ -3,21 +3,13 @@ package com.tylkowski.crc;
public class Main { public class Main {
public static void main(String[] args) { 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.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();
}
} }