1
0
forked from kalmar/DALGLI0

valid FCS output

This commit is contained in:
Hubert Tylkowski 2018-06-27 19:25:52 +02:00
parent a29f8b6947
commit 2e16ad8b73
5 changed files with 27 additions and 23 deletions

View File

@ -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++) {

View File

@ -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")) {