From 4c976710f92a9596da9de906bd6e76f96c909127 Mon Sep 17 00:00:00 2001 From: Hubert Tylkowski Date: Sat, 9 Jun 2018 14:04:27 +0200 Subject: [PATCH 1/8] Zadanie 02 --- 02-Wielomiany/src/DivisionErrorException.java | 5 + 02-Wielomiany/src/Main.java | 17 +++ .../src/MultiplierNotFoundException.java | 5 + 02-Wielomiany/src/PolynomialTask.java | 134 ++++++++++++++++++ 4 files changed, 161 insertions(+) create mode 100644 02-Wielomiany/src/DivisionErrorException.java create mode 100644 02-Wielomiany/src/Main.java create mode 100644 02-Wielomiany/src/MultiplierNotFoundException.java create mode 100644 02-Wielomiany/src/PolynomialTask.java diff --git a/02-Wielomiany/src/DivisionErrorException.java b/02-Wielomiany/src/DivisionErrorException.java new file mode 100644 index 0000000..7560c50 --- /dev/null +++ b/02-Wielomiany/src/DivisionErrorException.java @@ -0,0 +1,5 @@ +public class DivisionErrorException extends Throwable { + public DivisionErrorException() { + System.out.println("Division error!"); + } +} diff --git a/02-Wielomiany/src/Main.java b/02-Wielomiany/src/Main.java new file mode 100644 index 0000000..364a9b4 --- /dev/null +++ b/02-Wielomiany/src/Main.java @@ -0,0 +1,17 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class Main { + + public static void main(String[] args) throws DivisionErrorException, MultiplierNotFoundException { + //ex input in run console : 2 "1 1 1 0 1" "0 1 1" + int n = Integer.parseInt(args[0]); + List firstPolynomial = new ArrayList<>(); + Arrays.asList(args[1].split("\\s* \\s*")).forEach(factor -> firstPolynomial.add(Integer.valueOf(factor))); + List secondPolynomial = new ArrayList<>(); + Arrays.asList(args[2].split("\\s* \\s*")).forEach(factor -> secondPolynomial.add(Integer.valueOf(factor))); + PolynomialTask polynomialTask = new PolynomialTask(n, firstPolynomial, secondPolynomial); + polynomialTask.printAllValuesToStandardOutput(); + } +} diff --git a/02-Wielomiany/src/MultiplierNotFoundException.java b/02-Wielomiany/src/MultiplierNotFoundException.java new file mode 100644 index 0000000..3a9d83b --- /dev/null +++ b/02-Wielomiany/src/MultiplierNotFoundException.java @@ -0,0 +1,5 @@ +public class MultiplierNotFoundException extends Throwable { + public MultiplierNotFoundException() { + System.out.println("DivisionError"); + } +} diff --git a/02-Wielomiany/src/PolynomialTask.java b/02-Wielomiany/src/PolynomialTask.java new file mode 100644 index 0000000..1370891 --- /dev/null +++ b/02-Wielomiany/src/PolynomialTask.java @@ -0,0 +1,134 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +public class PolynomialTask { + private int n; + + private List firstPolynomial; + private List secondPolynomial; + + public PolynomialTask(int n, List firstPoly, List secondPoly) { + this.n = n; + this.firstPolynomial = firstPoly; + this.secondPolynomial = secondPoly; + } + + public Integer[] multiplyPolynomials(List firstPolynomial, List secondPolynomial) { + int[] multiplied = new int[firstPolynomial.size() + secondPolynomial.size() - 1]; + int sizeOfFirstPoly = firstPolynomial.size(); + int sizeOfSecondPoly = secondPolynomial.size(); + for (int i = 0; i < sizeOfFirstPoly; i++) { + for (int j = 0; j < sizeOfSecondPoly; j++) + multiplied[i + j] = (multiplied[i + j] + (firstPolynomial.get(i) * secondPolynomial.get(j))) % n; + } + return Arrays.stream(multiplied).boxed().toArray(Integer[]::new); + } + + public int[] moduloDividePolynomialsReturnQuotient(List firstPoly, List secondPoly) throws MultiplierNotFoundException, DivisionErrorException { + int[] quotient = new int[firstPoly.size() + secondPoly.size()]; + int firstPolyDegree = firstPoly.size() - 1; + int secondPolyDegree = secondPoly.size() - 1; + List polynomialAfterSubtract = firstPoly; + + + while (firstPolyDegree >= secondPolyDegree) { + polynomialAfterSubtract = calcQuotient(polynomialAfterSubtract, secondPoly, quotient); + firstPolyDegree = polynomialAfterSubtract.size() - 1; + } + + int[] remainder = new int[polynomialAfterSubtract.size()]; + for (int i = 0; i < polynomialAfterSubtract.size(); i++) { + remainder[i] = polynomialAfterSubtract.get(i); + } + return remainder; + } + + private List calcQuotient(List firstPoly, List secondPoly, int[] quotient) throws MultiplierNotFoundException, DivisionErrorException { + int firstPolyDegree = firstPoly.size() - 1; + int secondPolyDegree = secondPoly.size() - 1; + if (firstPolyDegree < secondPolyDegree) { + throw new DivisionErrorException(); + } + int quotientCoefficient; + if ((((float) firstPoly.get(firstPolyDegree) / (float) secondPoly.get(secondPolyDegree))) == Math.round(firstPoly.get(firstPolyDegree) / secondPoly.get(secondPolyDegree))) { + quotientCoefficient = firstPoly.get(firstPolyDegree) / secondPoly.get(secondPolyDegree); + } else { + quotientCoefficient = invElem(firstPoly.get(firstPolyDegree), secondPoly.get(secondPolyDegree)); + } + quotient[firstPolyDegree - secondPolyDegree] += quotientCoefficient; + List newPoly = generatePolyFromIndexAndValue(firstPolyDegree - secondPolyDegree, quotientCoefficient); + Integer[] multipliedPolynomials = multiplyPolynomials(newPoly, secondPoly); + List polynomialAfterFirstDivide = new ArrayList<>(Arrays.asList(multipliedPolynomials)); + + return removeUnnecessaryZeros(subtractTwoPolynomials(firstPoly, polynomialAfterFirstDivide)); + } + + private List removeUnnecessaryZeros(List polynomialAfterSubtract) { + int amountOfZeros = 0; + for (int i = polynomialAfterSubtract.size() - 1; i >= 0; i--) { + if (polynomialAfterSubtract.get(i) == 0) { + amountOfZeros++; + } else { + break; + } + } + + polynomialAfterSubtract = polynomialAfterSubtract.subList(0, polynomialAfterSubtract.size() - amountOfZeros); + return polynomialAfterSubtract; + } + + private List subtractTwoPolynomials(List firstPoly, List secondPoly) { + List subtractedPolynomial = new ArrayList<>(Collections.nCopies(firstPoly.size() + secondPoly.size(), 0)); + for (int index = firstPoly.size(); index >= 0; index--) { + if (index < secondPoly.size()) { + subtractedPolynomial.set(index, firstPoly.get(index) - secondPoly.get(index)); + parseNegativeElement(subtractedPolynomial, index); + } + } + return subtractedPolynomial; + } + + private void parseNegativeElement(List subtractedPolynomial, int index) { + while (subtractedPolynomial.get(index) < 0) + subtractedPolynomial.set(index, (subtractedPolynomial.get(index) * subtractedPolynomial.get(index)) % n); + } + + private List generatePolyFromIndexAndValue(int size, int quotientCoefficient) { + List poly = new ArrayList<>(Collections.nCopies(size + 1, 0)); + poly.set(size, quotientCoefficient); + return poly; + } + + private int invElem(int a, int b) throws MultiplierNotFoundException { + + for (int i = 0; i < n; i++) { + if (a == (b * i) % n) { + return i; + } + } + + throw new MultiplierNotFoundException(); + } + + public void printAllValuesToStandardOutput() throws DivisionErrorException, MultiplierNotFoundException { + List> values = new ArrayList<>(); + values.add(Arrays.asList(multiplyPolynomials(firstPolynomial, secondPolynomial))); + values.add(Arrays.stream(moduloDividePolynomialsReturnQuotient(firstPolynomial, secondPolynomial)).boxed().collect(Collectors.toList())); + try { + values.add(gcd(firstPolynomial, secondPolynomial)); + } catch (MultiplierNotFoundException e) { + } + System.out.println(values); + } + + private List gcd(List polyOne, List polyTwo) throws MultiplierNotFoundException, DivisionErrorException { + if (polyTwo.isEmpty()) return polyOne; + List poly = Arrays.stream(moduloDividePolynomialsReturnQuotient(polyOne, polyTwo)).boxed().collect(Collectors.toList()); + return gcd(polyTwo, poly); + + } + +} -- 2.20.1 From 966b225ba88e6b591400584a81baf9cae549988b Mon Sep 17 00:00:00 2001 From: Hubert Tylkowski Date: Tue, 19 Jun 2018 00:04:34 +0200 Subject: [PATCH 2/8] project started --- Zadanie-03/.idea/vcs.xml | 6 ++++++ Zadanie-03/src/com/tylkowski/crc/CrcTask.java | 13 ++++++++++++ Zadanie-03/src/com/tylkowski/crc/Main.java | 21 +++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 Zadanie-03/.idea/vcs.xml create mode 100644 Zadanie-03/src/com/tylkowski/crc/CrcTask.java create mode 100644 Zadanie-03/src/com/tylkowski/crc/Main.java diff --git a/Zadanie-03/.idea/vcs.xml b/Zadanie-03/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/Zadanie-03/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Zadanie-03/src/com/tylkowski/crc/CrcTask.java b/Zadanie-03/src/com/tylkowski/crc/CrcTask.java new file mode 100644 index 0000000..6791b26 --- /dev/null +++ b/Zadanie-03/src/com/tylkowski/crc/CrcTask.java @@ -0,0 +1,13 @@ +package com.tylkowski.crc; + +import java.util.Arrays; + +public class CrcTask { + private String message; + private String polyGenerator; + + public CrcTask(String message) { + this.message = message; + this.polyGenerator = "10001000000100001"; + } +} diff --git a/Zadanie-03/src/com/tylkowski/crc/Main.java b/Zadanie-03/src/com/tylkowski/crc/Main.java new file mode 100644 index 0000000..00dad62 --- /dev/null +++ b/Zadanie-03/src/com/tylkowski/crc/Main.java @@ -0,0 +1,21 @@ +package com.tylkowski.crc; + +public class Main { + + public static void main(String[] args) { + CrcTask crcTask = new CrcTask(toBinary(args[0])); + } + + 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(); + } +} -- 2.20.1 From 59cc5a61df3b050a982cfa7f9713d2615b2cad2e Mon Sep 17 00:00:00 2001 From: Hubert Tylkowski Date: Tue, 19 Jun 2018 00:45:51 +0200 Subject: [PATCH 3/8] poly generating & swapping --- Zadanie-03/Zadanie-03.iml | 12 +++++++ Zadanie-03/src/com/tylkowski/crc/CrcTask.java | 35 ++++++++++++++++--- 2 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 Zadanie-03/Zadanie-03.iml diff --git a/Zadanie-03/Zadanie-03.iml b/Zadanie-03/Zadanie-03.iml new file mode 100644 index 0000000..d5c0743 --- /dev/null +++ b/Zadanie-03/Zadanie-03.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/Zadanie-03/src/com/tylkowski/crc/CrcTask.java b/Zadanie-03/src/com/tylkowski/crc/CrcTask.java index 6791b26..cf67b17 100644 --- a/Zadanie-03/src/com/tylkowski/crc/CrcTask.java +++ b/Zadanie-03/src/com/tylkowski/crc/CrcTask.java @@ -1,13 +1,38 @@ package com.tylkowski.crc; -import java.util.Arrays; - public class CrcTask { private String message; - private String polyGenerator; + private short[] messageAsShortArray; + private short[] polyGenerator; public CrcTask(String message) { - this.message = message; - this.polyGenerator = "10001000000100001"; + this.message = message + "0000000000000000"; + convertMessageToBinaryShortArray(); + createGeneratingPoly(); + swapPolyValues(); + } + + private void convertMessageToBinaryShortArray() { + messageAsShortArray = new short[message.length()]; + for (int i = 0; i < message.length(); i++) { + if (message.charAt(i) == 48) { + messageAsShortArray[i] = 0; + } else { + messageAsShortArray[i] = 1; + } + } + } + + private void createGeneratingPoly() { + polyGenerator = new short[16]; + for (int i = 0; i < 16; i++) { + polyGenerator[i] = 0; + } + } + + private void swapPolyValues() { + for (int i = 0; i < polyGenerator.length; i++) { + messageAsShortArray[i] = (short) ((messageAsShortArray[i] + 1) % 2); + } } } -- 2.20.1 From 84c7c81153749bd03864cac7947af6447bc770cd Mon Sep 17 00:00:00 2001 From: Hubert Tylkowski Date: Wed, 20 Jun 2018 00:30:21 +0200 Subject: [PATCH 4/8] encode added! --- Zadanie-03/src/com/tylkowski/crc/CrcTask.java | 155 ++++++++++++++++-- Zadanie-03/src/com/tylkowski/crc/Main.java | 3 +- 2 files changed, 143 insertions(+), 15 deletions(-) diff --git a/Zadanie-03/src/com/tylkowski/crc/CrcTask.java b/Zadanie-03/src/com/tylkowski/crc/CrcTask.java index cf67b17..57d2e59 100644 --- a/Zadanie-03/src/com/tylkowski/crc/CrcTask.java +++ b/Zadanie-03/src/com/tylkowski/crc/CrcTask.java @@ -1,20 +1,125 @@ package com.tylkowski.crc; -public class CrcTask { +import java.util.Arrays; + +class CrcTask { private String message; private short[] messageAsShortArray; private short[] polyGenerator; + private String rawMessage; - public CrcTask(String message) { - this.message = message + "0000000000000000"; - convertMessageToBinaryShortArray(); - createGeneratingPoly(); - swapPolyValues(); + CrcTask(String message, String rawMessage) { + this.message = formatMessage(message) + "0000000000000000"; + this.rawMessage = rawMessage; + createGeneratingPolynomial(); + } + + 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() { 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) { messageAsShortArray[i] = 0; } else { @@ -23,16 +128,38 @@ public class CrcTask { } } - private void createGeneratingPoly() { - polyGenerator = new short[16]; - for (int i = 0; i < 16; i++) { + private short[] convertBinaryStringToShortArray(String binaryString) { + short[] shortArray = new short[binaryString.length()]; + for (int i = 0; i < binaryString.length(); i++) { + if (binaryString.charAt(i) == 48) { + shortArray[i] = 0; + } else { + shortArray[i] = 1; + } + } + return shortArray; + } + + private void createGeneratingPolynomial() { + 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); + } } - private void swapPolyValues() { - for (int i = 0; i < polyGenerator.length; i++) { - messageAsShortArray[i] = (short) ((messageAsShortArray[i] + 1) % 2); - } + String encode() { + convertMessageToBinaryShortArray(); + swapPolynomialValues(); + return rawMessage + generateFCS(); } } diff --git a/Zadanie-03/src/com/tylkowski/crc/Main.java b/Zadanie-03/src/com/tylkowski/crc/Main.java index 00dad62..3cc07b8 100644 --- a/Zadanie-03/src/com/tylkowski/crc/Main.java +++ b/Zadanie-03/src/com/tylkowski/crc/Main.java @@ -3,7 +3,8 @@ package com.tylkowski.crc; public class Main { 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) { -- 2.20.1 From 446d5f23062dd965c0b0409819c0d274a570fa79 Mon Sep 17 00:00:00 2001 From: Hubert Tylkowski Date: Wed, 20 Jun 2018 01:06:07 +0200 Subject: [PATCH 5/8] decode added! --- Zadanie-03/src/com/tylkowski/crc/CrcTask.java | 61 ++++++++++++++++--- Zadanie-03/src/com/tylkowski/crc/Main.java | 1 + 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/Zadanie-03/src/com/tylkowski/crc/CrcTask.java b/Zadanie-03/src/com/tylkowski/crc/CrcTask.java index 57d2e59..b6c9df4 100644 --- a/Zadanie-03/src/com/tylkowski/crc/CrcTask.java +++ b/Zadanie-03/src/com/tylkowski/crc/CrcTask.java @@ -53,7 +53,7 @@ class CrcTask { } short[] remainder = calcXOR(piece, polyGenerator); remainder = removeUnecessaryZeros(remainder); - createMessageFromRemainderAndPartFromOldMessage(remainder, remainder.length); + messageAsShortArray = createMessageFromRemainderAndPartFromOldMessage(remainder, messageAsShortArray); } @@ -61,12 +61,13 @@ class CrcTask { } - private void createMessageFromRemainderAndPartFromOldMessage(short[] remainder, int length) { - short[] tempArr = new short[remainder.length + messageAsShortArray.length - polyGenerator.length]; - System.arraycopy(remainder, 0, tempArr, 0, length); + 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); int diff = polyGenerator.length - remainder.length; - System.arraycopy(messageAsShortArray, length + diff, tempArr, length, messageAsShortArray.length - diff - length); - messageAsShortArray = tempArr; + System.arraycopy(msg, remainder.length + diff, tempArr, remainder.length, msg.length - diff - remainder.length); + msg = tempArr; + return msg; } private short[] removeUnecessaryZeros(short[] remainder) { @@ -151,15 +152,57 @@ class CrcTask { polyGenerator[16] = 1; } - private void swapPolynomialValues() { + private short[] swapPolynomialValues(short[] poly) { 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() { convertMessageToBinaryShortArray(); - swapPolynomialValues(); + messageAsShortArray = swapPolynomialValues(messageAsShortArray); 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(); + } } diff --git a/Zadanie-03/src/com/tylkowski/crc/Main.java b/Zadanie-03/src/com/tylkowski/crc/Main.java index 3cc07b8..77f226e 100644 --- a/Zadanie-03/src/com/tylkowski/crc/Main.java +++ b/Zadanie-03/src/com/tylkowski/crc/Main.java @@ -5,6 +5,7 @@ public class Main { public static void main(String[] args) { CrcTask crcTask = new CrcTask(toBinary(args[0]), args[0]); System.out.println(crcTask.encode()); + System.out.println(crcTask.decode(toBinary(crcTask.encode()))); } private static String toBinary(String s) { -- 2.20.1 From 744f9793c571e9795aa61b77b966d4777f1dd69b Mon Sep 17 00:00:00 2001 From: Hubert Tylkowski Date: Wed, 20 Jun 2018 01:13:23 +0200 Subject: [PATCH 6/8] small code refactor --- Zadanie-03/src/com/tylkowski/crc/CrcTask.java | 16 ++++++++++++++- Zadanie-03/src/com/tylkowski/crc/Main.java | 20 ++++++------------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/Zadanie-03/src/com/tylkowski/crc/CrcTask.java b/Zadanie-03/src/com/tylkowski/crc/CrcTask.java index b6c9df4..7e36a45 100644 --- a/Zadanie-03/src/com/tylkowski/crc/CrcTask.java +++ b/Zadanie-03/src/com/tylkowski/crc/CrcTask.java @@ -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); diff --git a/Zadanie-03/src/com/tylkowski/crc/Main.java b/Zadanie-03/src/com/tylkowski/crc/Main.java index 77f226e..c71d643 100644 --- a/Zadanie-03/src/com/tylkowski/crc/Main.java +++ b/Zadanie-03/src/com/tylkowski/crc/Main.java @@ -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(); - } + } -- 2.20.1 From 841b8b4590436f89e15bac91492cb2852824c5d0 Mon Sep 17 00:00:00 2001 From: Hubert Tylkowski Date: Wed, 20 Jun 2018 11:45:59 +0200 Subject: [PATCH 7/8] charset update --- Zadanie-03/.idea/description.html | 1 + Zadanie-03/.idea/misc.xml | 12 + Zadanie-03/.idea/modules.xml | 8 + Zadanie-03/.idea/project-template.xml | 3 + Zadanie-03/.idea/workspace.xml | 387 ++++++++++++++++++ .../com/tylkowski/crc/CrcTask.class | Bin 0 -> 6441 bytes .../Zadanie-03/com/tylkowski/crc/Main.class | Bin 0 -> 1086 bytes Zadanie-03/src/com/tylkowski/crc/CrcTask.java | 88 ++-- Zadanie-03/src/com/tylkowski/crc/Main.java | 19 +- 9 files changed, 473 insertions(+), 45 deletions(-) create mode 100644 Zadanie-03/.idea/description.html create mode 100644 Zadanie-03/.idea/misc.xml create mode 100644 Zadanie-03/.idea/modules.xml create mode 100644 Zadanie-03/.idea/project-template.xml create mode 100644 Zadanie-03/.idea/workspace.xml create mode 100644 Zadanie-03/out/production/Zadanie-03/com/tylkowski/crc/CrcTask.class create mode 100644 Zadanie-03/out/production/Zadanie-03/com/tylkowski/crc/Main.class diff --git a/Zadanie-03/.idea/description.html b/Zadanie-03/.idea/description.html new file mode 100644 index 0000000..db5f129 --- /dev/null +++ b/Zadanie-03/.idea/description.html @@ -0,0 +1 @@ +Simple Java application that includes a class with main() method \ No newline at end of file diff --git a/Zadanie-03/.idea/misc.xml b/Zadanie-03/.idea/misc.xml new file mode 100644 index 0000000..5d75313 --- /dev/null +++ b/Zadanie-03/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Zadanie-03/.idea/modules.xml b/Zadanie-03/.idea/modules.xml new file mode 100644 index 0000000..5982ee3 --- /dev/null +++ b/Zadanie-03/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Zadanie-03/.idea/project-template.xml b/Zadanie-03/.idea/project-template.xml new file mode 100644 index 0000000..1f08b88 --- /dev/null +++ b/Zadanie-03/.idea/project-template.xml @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/Zadanie-03/.idea/workspace.xml b/Zadanie-03/.idea/workspace.xml new file mode 100644 index 0000000..65c30df --- /dev/null +++ b/Zadanie-03/.idea/workspace.xml @@ -0,0 +1,387 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + System.out.println + for + + System + ch + shot + gen_deg + char + swa + chunk + toBin + toBinaryStr + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - @@ -144,8 +141,7 @@ - @@ -244,11 +248,11 @@ - + - + @@ -276,9 +280,27 @@ - + + + + + + + + + + + + + + + + + @@ -366,10 +388,24 @@ + + + + + + + + + + + + + + - - + + @@ -378,8 +414,8 @@ - - + + diff --git a/Zadanie-03/src/com/tylkowski/crc/CrcTask.java b/Zadanie-03/src/com/tylkowski/crc/CrcTask.java index 74a3099..eb9a506 100644 --- a/Zadanie-03/src/com/tylkowski/crc/CrcTask.java +++ b/Zadanie-03/src/com/tylkowski/crc/CrcTask.java @@ -18,10 +18,10 @@ class CrcTask { } private void setEncoding() throws NoSuchFieldException, IllegalAccessException { - System.setProperty("file.encoding","ISO-8859-1"); + System.setProperty("file.encoding", "ISO-8859-1"); Field charset = Charset.class.getDeclaredField("defaultCharset"); charset.setAccessible(true); - charset.set(null,null); + charset.set(null, null); } private String formatMessage(String message) { @@ -65,8 +65,6 @@ class CrcTask { remainder = removeUnecessaryZeros(remainder); messageAsShortArray = createMessageFromRemainderAndPartFromOldMessage(remainder, messageAsShortArray); } - - } } @@ -111,7 +109,6 @@ class CrcTask { private char getCharFromShortArray(short[] piece, int from, int to) { short[] sign = Arrays.copyOfRange(piece, from, to); int character = Integer.parseInt(shortArrayToBinaryString(sign), 2); -// System.out.println(character); return (char) character; } @@ -165,30 +162,23 @@ class CrcTask { } private String letterToBinaryString(char letter) { -// System.out.println("lt: " + letter + " " + (int) letter); int a = (int) letter; -// if (a == 63) a = 157; - System.out.println("letter in int: " + a); - StringBuilder binaryString = new StringBuilder(Integer.toBinaryString(a)); - while (binaryString.length() % 8 != 0) { binaryString.insert(0, "0"); } -// System.out.println("bs" + binaryString.toString()); return binaryString.toString(); } boolean decode(String encodedString) { encodedString = toBinaryString(encodedString); - encodedString = fillPolyTo8(encodedString); + encodedString = fillPoly(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 { @@ -208,7 +198,6 @@ class CrcTask { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < encodedString.length(); i++) { stringBuilder.append(letterToBinaryString(encodedString.charAt(i))); - } return stringBuilder.toString(); } @@ -222,7 +211,7 @@ class CrcTask { return false; } - private String fillPolyTo8(String encodedString) { + private String fillPoly(String encodedString) { StringBuilder stringBuilder = new StringBuilder(encodedString); while (stringBuilder.length() % 8 != 0) { stringBuilder.insert(0, "0"); diff --git a/Zadanie-03/src/com/tylkowski/crc/Main.java b/Zadanie-03/src/com/tylkowski/crc/Main.java index 1ef0595..b85f350 100644 --- a/Zadanie-03/src/com/tylkowski/crc/Main.java +++ b/Zadanie-03/src/com/tylkowski/crc/Main.java @@ -3,10 +3,10 @@ package com.tylkowski.crc; public class Main { public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException { - // use valid character encoding ISO-8859-1 - // in command line type "1" for mode and "a" for message - //example "1" "a" -> this will encode string "a" and return FCS - // example "2" "abc" -> this will decode string "abc" and return true if it is valid or false if not + // 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")) { //create FCS -- 2.20.1