diff --git a/Zadanie-03/out/artifacts/Zadanie_03_jar/Zadanie-03.jar b/Zadanie-03/out/artifacts/Zadanie_03_jar/Zadanie-03.jar index 8bd6df9..5d76ae0 100644 Binary files a/Zadanie-03/out/artifacts/Zadanie_03_jar/Zadanie-03.jar and b/Zadanie-03/out/artifacts/Zadanie_03_jar/Zadanie-03.jar differ diff --git a/Zadanie-03/out/production/Zadanie-03/com/tylkowski/crc/CrcTask.class b/Zadanie-03/out/production/Zadanie-03/com/tylkowski/crc/CrcTask.class index cf26b3d..13d1da1 100644 Binary files a/Zadanie-03/out/production/Zadanie-03/com/tylkowski/crc/CrcTask.class and b/Zadanie-03/out/production/Zadanie-03/com/tylkowski/crc/CrcTask.class differ diff --git a/Zadanie-03/out/production/Zadanie-03/com/tylkowski/crc/Main.class b/Zadanie-03/out/production/Zadanie-03/com/tylkowski/crc/Main.class index daedb27..86a430a 100644 Binary files a/Zadanie-03/out/production/Zadanie-03/com/tylkowski/crc/Main.class and b/Zadanie-03/out/production/Zadanie-03/com/tylkowski/crc/Main.class differ diff --git a/Zadanie-03/src/com/tylkowski/crc/CrcTask.java b/Zadanie-03/src/com/tylkowski/crc/CrcTask.java index 6bf7471..1b02ada 100644 --- a/Zadanie-03/src/com/tylkowski/crc/CrcTask.java +++ b/Zadanie-03/src/com/tylkowski/crc/CrcTask.java @@ -1,7 +1,5 @@ package com.tylkowski.crc; -import java.lang.reflect.Field; -import java.nio.charset.Charset; import java.util.Arrays; class CrcTask { @@ -10,19 +8,12 @@ class CrcTask { private short[] polyGenerator; private String rawMessage; - CrcTask(String message, String rawMessage) throws NoSuchFieldException, IllegalAccessException { - setEncoding(); + CrcTask(String message) { + this.rawMessage = message; this.message = formatMessage(toBinaryString(message)) + "0000000000000000"; - this.rawMessage = rawMessage; createGeneratingPolynomial(); } - private void setEncoding() throws NoSuchFieldException, IllegalAccessException { - System.setProperty("file.encoding", "ISO-8859-1"); - Field charset = Charset.class.getDeclaredField("defaultCharset"); - charset.setAccessible(true); - charset.set(null, null); - } private String formatMessage(String message) { int firstNonZeroVal = 0; @@ -59,7 +50,7 @@ class CrcTask { 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); + return createTwoCharsOfFCS(piece); } short[] remainder = calcXOR(piece, polyGenerator); remainder = removeUnecessaryZeros(remainder); @@ -69,6 +60,17 @@ class CrcTask { } + private String createTwoCharsOfFCS(short[] piece) { + short[] firstPiece = Arrays.copyOfRange(piece, 0, 8); + short[] secondPiece = Arrays.copyOfRange(piece, piece.length - 8, piece.length); + short[] mergedPieces = new short[firstPiece.length + secondPiece.length]; + for (int i = 0; i < firstPiece.length; i++) { + mergedPieces[i] = firstPiece[i]; + mergedPieces[i + 8] = secondPiece[i]; + } + return "0x" + Integer.toHexString(Integer.parseInt(shortArrayToBinaryString(mergedPieces), 2)); + } + private short[] createMessageFromRemainderAndPartFromOldMessage(short[] remainder, short[] msg) { short[] tempArr = new short[remainder.length + msg.length - polyGenerator.length]; System.arraycopy(remainder, 0, tempArr, 0, remainder.length); @@ -106,12 +108,6 @@ class CrcTask { return binaryString.toString(); } - private char getCharFromShortArray(short[] piece, int from, int to) { - short[] sign = Arrays.copyOfRange(piece, from, to); - int character = Integer.parseInt(shortArrayToBinaryString(sign), 2); - return (char) character; - } - private void fillPolynomial(short[] piece) { while (piece.length % 8 != 0) { piece = addZeroAtBeginningOfArray(piece); @@ -171,7 +167,10 @@ class CrcTask { } boolean decode(String encodedString) { + String fcs = encodedString.substring(encodedString.indexOf("0x"), encodedString.length()); + encodedString = encodedString.replace(fcs, ""); encodedString = toBinaryString(encodedString); + encodedString = encodedString + toBinaryStringFromHexValue(fcs); encodedString = fillPoly(encodedString); short[] encodedShortArray = convertBinaryStringToShortArray(encodedString); encodedShortArray = swapPolynomialValues(encodedShortArray); @@ -194,6 +193,11 @@ class CrcTask { } } + private String toBinaryStringFromHexValue(String hexString) { + hexString = hexString.substring(2, hexString.length()); + return String.valueOf(Integer.toBinaryString(Integer.parseInt(hexString, 16))); + } + private String toBinaryString(String encodedString) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < encodedString.length(); i++) { diff --git a/Zadanie-03/src/com/tylkowski/crc/Main.java b/Zadanie-03/src/com/tylkowski/crc/Main.java index b85f350..3911551 100644 --- a/Zadanie-03/src/com/tylkowski/crc/Main.java +++ b/Zadanie-03/src/com/tylkowski/crc/Main.java @@ -2,13 +2,13 @@ package com.tylkowski.crc; public class Main { - public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException { + public static void main(String[] args) { // in command line type "1" for mode and "b" for message // example "1" "b" -> this will encode string "b" and return FCS - // example "2" "bXY" -> this will decode string "bXY" and return true if it is valid or false if not - // X and Y - 1 and 2 character of FCS - CrcTask crcTask = new CrcTask(args[1], args[1]); - if(args[0].equals("1")) { + // example "2" "bX" -> this will decode string "bX" and return true if it is valid or false if not + // X - FCS + CrcTask crcTask = new CrcTask(args[1]); + if (args[0].equals("1")) { //create FCS System.out.println(crcTask.encode()); } else if (args[0].equals("2")) {