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 @@
+
+ IJ_BASE_PACKAGE
+
\ 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
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 1528990184795
+
+
+ 1528990184795
+
+
+
+
+
+
+
+
+
+ 1529359475020
+
+
+
+ 1529359475021
+
+
+ 1529361951813
+
+
+
+ 1529361951813
+
+
+ 1529447421427
+
+
+
+ 1529447421427
+
+
+ 1529449567531
+
+
+
+ 1529449567531
+
+
+ 1529450003665
+
+
+
+ 1529450003665
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
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
new file mode 100644
index 0000000..88a50c3
Binary files /dev/null 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
new file mode 100644
index 0000000..daedb27
Binary files /dev/null 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 7e36a45..74a3099 100644
--- a/Zadanie-03/src/com/tylkowski/crc/CrcTask.java
+++ b/Zadanie-03/src/com/tylkowski/crc/CrcTask.java
@@ -1,5 +1,7 @@
package com.tylkowski.crc;
+import java.lang.reflect.Field;
+import java.nio.charset.Charset;
import java.util.Arrays;
class CrcTask {
@@ -8,23 +10,18 @@ class CrcTask {
private short[] polyGenerator;
private String rawMessage;
- CrcTask(String message, String rawMessage) {
- this.message = formatMessage(toBinary(message)) + "0000000000000000";
+ CrcTask(String message, String rawMessage) throws NoSuchFieldException, IllegalAccessException {
+ setEncoding();
+ this.message = formatMessage(toBinaryString(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 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) {
@@ -96,8 +93,8 @@ class CrcTask {
return Arrays.copyOfRange(remainder, firstNonZeroVal, remainder.length);
}
- private short[] calcXOR(short[] chunk, short[] polyGenerator) {
- int a = Integer.parseInt(shortArrayToBinaryString(chunk), 2);
+ private short[] calcXOR(short[] piece, short[] polyGenerator) {
+ int a = Integer.parseInt(shortArrayToBinaryString(piece), 2);
int b = Integer.parseInt(shortArrayToBinaryString(polyGenerator), 2);
String binaryString = Integer.toBinaryString(a ^ b);
return convertBinaryStringToShortArray(binaryString);
@@ -111,35 +108,24 @@ class CrcTask {
return binaryString.toString();
}
- private char getCharFromShortArray(short[] chunk, int from, int to) {
- short[] sign = Arrays.copyOfRange(chunk, from, to);
+ 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;
}
- private void fillPolynomial(short[] chunk) {
- while (chunk.length % 8 != 0) {
- chunk = addZeroAtBeginningOfArray(chunk);
+ private void fillPolynomial(short[] piece) {
+ while (piece.length % 8 != 0) {
+ piece = addZeroAtBeginningOfArray(piece);
}
}
- 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 < polyGenerator.length; i++) {
- if (message.charAt(i) == 48) {
- messageAsShortArray[i] = 0;
- } else {
- messageAsShortArray[i] = 1;
- }
- }
+ private short[] addZeroAtBeginningOfArray(short[] piece) {
+ short[] tempArray = new short[piece.length + 1];
+ tempArray[0] = 0;
+ System.arraycopy(piece, 0, tempArray, 1, piece.length);
+ return tempArray;
}
private short[] convertBinaryStringToShortArray(String binaryString) {
@@ -173,18 +159,33 @@ class CrcTask {
}
String encode() {
- convertMessageToBinaryShortArray();
+ messageAsShortArray = convertBinaryStringToShortArray(message);
messageAsShortArray = swapPolynomialValues(messageAsShortArray);
return rawMessage + generateFCS();
}
+ 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 = toBinary(encodedString);
+ encodedString = toBinaryString(encodedString);
encodedString = fillPolyTo8(encodedString);
short[] encodedShortArray = convertBinaryStringToShortArray(encodedString);
encodedShortArray = swapPolynomialValues(encodedShortArray);
while (true) {
- if (shortArrayContains(encodedShortArray, 1)) {
+ if (!shortArrayContains(encodedShortArray, 1)) {
return true;
}
@@ -203,6 +204,15 @@ class CrcTask {
}
}
+ private String toBinaryString(String encodedString) {
+ StringBuilder stringBuilder = new StringBuilder();
+ for (int i = 0; i < encodedString.length(); i++) {
+ stringBuilder.append(letterToBinaryString(encodedString.charAt(i)));
+
+ }
+ return stringBuilder.toString();
+ }
+
private boolean shortArrayContains(short[] encodedShortArray, int value) {
for (short item : encodedShortArray) {
if (item == value) {
diff --git a/Zadanie-03/src/com/tylkowski/crc/Main.java b/Zadanie-03/src/com/tylkowski/crc/Main.java
index c71d643..1ef0595 100644
--- a/Zadanie-03/src/com/tylkowski/crc/Main.java
+++ b/Zadanie-03/src/com/tylkowski/crc/Main.java
@@ -2,12 +2,19 @@ package com.tylkowski.crc;
public class Main {
- public static void main(String[] args) {
- // ex. in command line type "a"
- CrcTask crcTask = new CrcTask(args[0], args[0]);
- System.out.println(crcTask.encode());
- String input = crcTask.encode();
- if (input.length() >= 3) System.out.println(crcTask.decode(input));
+ 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
+ CrcTask crcTask = new CrcTask(args[1], args[1]);
+ if(args[0].equals("1")) {
+ //create FCS
+ System.out.println(crcTask.encode());
+ } else if (args[0].equals("2")) {
+ // check fcs
+ if (args[1].length() >= 3) System.out.println(crcTask.decode(args[1]));
+ }
}