1
0
forked from kalmar/DALGLI0

decode added!

This commit is contained in:
Hubert Tylkowski 2018-06-20 01:06:07 +02:00
parent 84c7c81153
commit 446d5f2306
2 changed files with 53 additions and 9 deletions

View File

@ -53,7 +53,7 @@ class CrcTask {
} }
short[] remainder = calcXOR(piece, polyGenerator); short[] remainder = calcXOR(piece, polyGenerator);
remainder = removeUnecessaryZeros(remainder); remainder = removeUnecessaryZeros(remainder);
createMessageFromRemainderAndPartFromOldMessage(remainder, remainder.length); messageAsShortArray = createMessageFromRemainderAndPartFromOldMessage(remainder, messageAsShortArray);
} }
@ -61,12 +61,13 @@ class CrcTask {
} }
private void createMessageFromRemainderAndPartFromOldMessage(short[] remainder, int length) { private short[] createMessageFromRemainderAndPartFromOldMessage(short[] remainder, short[] msg) {
short[] tempArr = new short[remainder.length + messageAsShortArray.length - polyGenerator.length]; short[] tempArr = new short[remainder.length + msg.length - polyGenerator.length];
System.arraycopy(remainder, 0, tempArr, 0, length); System.arraycopy(remainder, 0, tempArr, 0, remainder.length);
int diff = polyGenerator.length - remainder.length; int diff = polyGenerator.length - remainder.length;
System.arraycopy(messageAsShortArray, length + diff, tempArr, length, messageAsShortArray.length - diff - length); System.arraycopy(msg, remainder.length + diff, tempArr, remainder.length, msg.length - diff - remainder.length);
messageAsShortArray = tempArr; msg = tempArr;
return msg;
} }
private short[] removeUnecessaryZeros(short[] remainder) { private short[] removeUnecessaryZeros(short[] remainder) {
@ -151,15 +152,57 @@ class CrcTask {
polyGenerator[16] = 1; polyGenerator[16] = 1;
} }
private void swapPolynomialValues() { private short[] swapPolynomialValues(short[] poly) {
for (int i = 0; i < polyGenerator.length - 1; i++) { for (int i = 0; i < polyGenerator.length - 1; i++) {
messageAsShortArray[i] = (short) ((messageAsShortArray[i] + 1) % 2); poly[i] = (short) ((poly[i] + 1) % 2);
} }
return poly;
} }
String encode() { String encode() {
convertMessageToBinaryShortArray(); convertMessageToBinaryShortArray();
swapPolynomialValues(); messageAsShortArray = swapPolynomialValues(messageAsShortArray);
return rawMessage + generateFCS(); return rawMessage + generateFCS();
} }
boolean decode(String encodedString) {
encodedString = fillPolyTo8(encodedString);
short[] encodedShortArray = convertBinaryStringToShortArray(encodedString);
encodedShortArray = swapPolynomialValues(encodedShortArray);
while (true) {
if (shortArrayContains(encodedShortArray, 1)) {
return true;
}
if (encodedShortArray[0] == 0) {
encodedShortArray = Arrays.copyOfRange(encodedShortArray, 1, encodedShortArray.length);
} else {
short[] piece = Arrays.copyOfRange(encodedShortArray, 0, Math.min(polyGenerator.length, encodedShortArray.length));
if (piece.length < polyGenerator.length) {
return false;
}
short[] remainder = calcXOR(piece, polyGenerator);
remainder = removeUnecessaryZeros(remainder);
encodedShortArray = createMessageFromRemainderAndPartFromOldMessage(remainder, encodedShortArray);
}
}
}
private boolean shortArrayContains(short[] encodedShortArray, int value) {
for (short item : encodedShortArray) {
if (item == value) {
return true;
}
}
return false;
}
private String fillPolyTo8(String encodedString) {
StringBuilder stringBuilder = new StringBuilder(encodedString);
while (stringBuilder.length() % 8 != 0) {
stringBuilder.insert(0, "0");
}
return stringBuilder.toString();
}
} }

View File

@ -5,6 +5,7 @@ public class Main {
public static void main(String[] args) { public static void main(String[] args) {
CrcTask crcTask = new CrcTask(toBinary(args[0]), args[0]); CrcTask crcTask = new CrcTask(toBinary(args[0]), args[0]);
System.out.println(crcTask.encode()); System.out.println(crcTask.encode());
System.out.println(crcTask.decode(toBinary(crcTask.encode())));
} }
private static String toBinary(String s) { private static String toBinary(String s) {