Zadananie 3 - CRC #26

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

View File

@ -1,20 +1,125 @@
package com.tylkowski.crc; package com.tylkowski.crc;
public class CrcTask { import java.util.Arrays;
class CrcTask {
private String message; private String message;
private short[] messageAsShortArray; private short[] messageAsShortArray;
private short[] polyGenerator; private short[] polyGenerator;
private String rawMessage;
public CrcTask(String message) { CrcTask(String message, String rawMessage) {
this.message = message + "0000000000000000"; this.message = formatMessage(message) + "0000000000000000";
convertMessageToBinaryShortArray(); this.rawMessage = rawMessage;
createGeneratingPoly(); createGeneratingPolynomial();
swapPolyValues(); }
private String formatMessage(String message) {
int firstNonZeroVal = 0;
boolean found = false;
StringBuilder validString = new StringBuilder();
while (!found && firstNonZeroVal < message.length()) {
if (message.charAt(firstNonZeroVal) == 48) {
firstNonZeroVal++;
} else {
found = true;
}
}
for (int i = firstNonZeroVal; i < message.length(); i++) {
if (message.charAt(i) == 48) {
validString.append(0);
} else {
validString.append(1);
}
}
StringBuilder msg = new StringBuilder(validString.toString());
while (msg.length() % 8 != 0) {
msg.insert(0, "0");
}
return msg.toString();
}
private String generateFCS() {
while (true) {
if (messageAsShortArray[0] == 0) {
messageAsShortArray = Arrays.copyOfRange(messageAsShortArray, 1, messageAsShortArray.length);
} else {
short[] piece = Arrays.copyOfRange(messageAsShortArray, 0, Math.min(polyGenerator.length, messageAsShortArray.length));
if (piece.length < polyGenerator.length) {
fillPolynomial(piece);
return "" + getCharFromShortArray(piece, 0, 8) + getCharFromShortArray(piece, piece.length - 8, piece.length);
}
short[] remainder = calcXOR(piece, polyGenerator);
remainder = removeUnecessaryZeros(remainder);
createMessageFromRemainderAndPartFromOldMessage(remainder, remainder.length);
}
}
}
private void createMessageFromRemainderAndPartFromOldMessage(short[] remainder, int length) {
short[] tempArr = new short[remainder.length + messageAsShortArray.length - polyGenerator.length];
System.arraycopy(remainder, 0, tempArr, 0, length);
int diff = polyGenerator.length - remainder.length;
System.arraycopy(messageAsShortArray, length + diff, tempArr, length, messageAsShortArray.length - diff - length);
messageAsShortArray = tempArr;
}
private short[] removeUnecessaryZeros(short[] remainder) {
int firstNonZeroVal = 0;
boolean found = false;
while (!found && firstNonZeroVal < remainder.length) {
if (remainder[firstNonZeroVal] == 0) {
firstNonZeroVal++;
} else {
found = true;
}
}
return Arrays.copyOfRange(remainder, firstNonZeroVal, remainder.length);
}
private short[] calcXOR(short[] chunk, short[] polyGenerator) {
int a = Integer.parseInt(shortArrayToBinaryString(chunk), 2);
int b = Integer.parseInt(shortArrayToBinaryString(polyGenerator), 2);
String binaryString = Integer.toBinaryString(a ^ b);
return convertBinaryStringToShortArray(binaryString);
}
private String shortArrayToBinaryString(short[] value) {
StringBuilder binaryString = new StringBuilder();
for (Short a : value) {
binaryString.append(a);
}
return binaryString.toString();
}
private char getCharFromShortArray(short[] chunk, int from, int to) {
short[] sign = Arrays.copyOfRange(chunk, from, to);
int character = Integer.parseInt(shortArrayToBinaryString(sign), 2);
// System.out.println(character);
return (char) character;
}
private void fillPolynomial(short[] chunk) {
while (chunk.length % 8 != 0) {
chunk = addZeroAtBeginningOfArray(chunk);
}
}
private short[] addZeroAtBeginningOfArray(short[] chunk) {
short[] letterTemp = new short[chunk.length + 1];
letterTemp[0] = 0;
System.arraycopy(chunk, 0, letterTemp, 1, chunk.length);
return letterTemp;
} }
private void convertMessageToBinaryShortArray() { private void convertMessageToBinaryShortArray() {
messageAsShortArray = new short[message.length()]; messageAsShortArray = new short[message.length()];
for (int i = 0; i < message.length(); i++) { for (int i = 0; i < polyGenerator.length; i++) {
if (message.charAt(i) == 48) { if (message.charAt(i) == 48) {
messageAsShortArray[i] = 0; messageAsShortArray[i] = 0;
} else { } else {
@ -23,16 +128,38 @@ public class CrcTask {
} }
} }
private void createGeneratingPoly() { private short[] convertBinaryStringToShortArray(String binaryString) {
polyGenerator = new short[16]; short[] shortArray = new short[binaryString.length()];
for (int i = 0; i < 16; i++) { for (int i = 0; i < binaryString.length(); i++) {
polyGenerator[i] = 0; if (binaryString.charAt(i) == 48) {
shortArray[i] = 0;
} else {
shortArray[i] = 1;
} }
} }
return shortArray;
}
private void swapPolyValues() { private void createGeneratingPolynomial() {
for (int i = 0; i < polyGenerator.length; i++) { polyGenerator = new short[17];
for (int i = 0; i < 17; i++) {
polyGenerator[i] = 0;
}
polyGenerator[0] = 1;
polyGenerator[4] = 1;
polyGenerator[11] = 1;
polyGenerator[16] = 1;
}
private void swapPolynomialValues() {
for (int i = 0; i < polyGenerator.length - 1; i++) {
messageAsShortArray[i] = (short) ((messageAsShortArray[i] + 1) % 2); messageAsShortArray[i] = (short) ((messageAsShortArray[i] + 1) % 2);
} }
} }
String encode() {
convertMessageToBinaryShortArray();
swapPolynomialValues();
return rawMessage + generateFCS();
}
} }

View File

@ -3,7 +3,8 @@ 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])); CrcTask crcTask = new CrcTask(toBinary(args[0]), args[0]);
System.out.println(crcTask.encode());
} }
private static String toBinary(String s) { private static String toBinary(String s) {