forked from kalmar/DALGLI0
valid FCS output
This commit is contained in:
parent
a29f8b6947
commit
2e16ad8b73
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,7 +1,5 @@
|
|||||||
package com.tylkowski.crc;
|
package com.tylkowski.crc;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
|
||||||
import java.nio.charset.Charset;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
class CrcTask {
|
class CrcTask {
|
||||||
@ -10,19 +8,12 @@ class CrcTask {
|
|||||||
private short[] polyGenerator;
|
private short[] polyGenerator;
|
||||||
private String rawMessage;
|
private String rawMessage;
|
||||||
|
|
||||||
CrcTask(String message, String rawMessage) throws NoSuchFieldException, IllegalAccessException {
|
CrcTask(String message) {
|
||||||
setEncoding();
|
this.rawMessage = message;
|
||||||
this.message = formatMessage(toBinaryString(message)) + "0000000000000000";
|
this.message = formatMessage(toBinaryString(message)) + "0000000000000000";
|
||||||
this.rawMessage = rawMessage;
|
|
||||||
createGeneratingPolynomial();
|
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) {
|
private String formatMessage(String message) {
|
||||||
int firstNonZeroVal = 0;
|
int firstNonZeroVal = 0;
|
||||||
@ -59,7 +50,7 @@ class CrcTask {
|
|||||||
short[] piece = Arrays.copyOfRange(messageAsShortArray, 0, Math.min(polyGenerator.length, messageAsShortArray.length));
|
short[] piece = Arrays.copyOfRange(messageAsShortArray, 0, Math.min(polyGenerator.length, messageAsShortArray.length));
|
||||||
if (piece.length < polyGenerator.length) {
|
if (piece.length < polyGenerator.length) {
|
||||||
fillPolynomial(piece);
|
fillPolynomial(piece);
|
||||||
return "" + getCharFromShortArray(piece, 0, 8) + getCharFromShortArray(piece, piece.length - 8, piece.length);
|
return createTwoCharsOfFCS(piece);
|
||||||
}
|
}
|
||||||
short[] remainder = calcXOR(piece, polyGenerator);
|
short[] remainder = calcXOR(piece, polyGenerator);
|
||||||
remainder = removeUnecessaryZeros(remainder);
|
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) {
|
private short[] createMessageFromRemainderAndPartFromOldMessage(short[] remainder, short[] msg) {
|
||||||
short[] tempArr = new short[remainder.length + msg.length - polyGenerator.length];
|
short[] tempArr = new short[remainder.length + msg.length - polyGenerator.length];
|
||||||
System.arraycopy(remainder, 0, tempArr, 0, remainder.length);
|
System.arraycopy(remainder, 0, tempArr, 0, remainder.length);
|
||||||
@ -106,12 +108,6 @@ class CrcTask {
|
|||||||
return binaryString.toString();
|
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) {
|
private void fillPolynomial(short[] piece) {
|
||||||
while (piece.length % 8 != 0) {
|
while (piece.length % 8 != 0) {
|
||||||
piece = addZeroAtBeginningOfArray(piece);
|
piece = addZeroAtBeginningOfArray(piece);
|
||||||
@ -171,7 +167,10 @@ class CrcTask {
|
|||||||
}
|
}
|
||||||
|
|
||||||
boolean decode(String encodedString) {
|
boolean decode(String encodedString) {
|
||||||
|
String fcs = encodedString.substring(encodedString.indexOf("0x"), encodedString.length());
|
||||||
|
encodedString = encodedString.replace(fcs, "");
|
||||||
encodedString = toBinaryString(encodedString);
|
encodedString = toBinaryString(encodedString);
|
||||||
|
encodedString = encodedString + toBinaryStringFromHexValue(fcs);
|
||||||
encodedString = fillPoly(encodedString);
|
encodedString = fillPoly(encodedString);
|
||||||
short[] encodedShortArray = convertBinaryStringToShortArray(encodedString);
|
short[] encodedShortArray = convertBinaryStringToShortArray(encodedString);
|
||||||
encodedShortArray = swapPolynomialValues(encodedShortArray);
|
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) {
|
private String toBinaryString(String encodedString) {
|
||||||
StringBuilder stringBuilder = new StringBuilder();
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
for (int i = 0; i < encodedString.length(); i++) {
|
for (int i = 0; i < encodedString.length(); i++) {
|
||||||
|
@ -2,13 +2,13 @@ package com.tylkowski.crc;
|
|||||||
|
|
||||||
public class Main {
|
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
|
// in command line type "1" for mode and "b" for message
|
||||||
// example "1" "b" -> this will encode string "b" and return FCS
|
// 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
|
// example "2" "bX" -> this will decode string "bX" and return true if it is valid or false if not
|
||||||
// X and Y - 1 and 2 character of FCS
|
// X - FCS
|
||||||
CrcTask crcTask = new CrcTask(args[1], args[1]);
|
CrcTask crcTask = new CrcTask(args[1]);
|
||||||
if(args[0].equals("1")) {
|
if (args[0].equals("1")) {
|
||||||
//create FCS
|
//create FCS
|
||||||
System.out.println(crcTask.encode());
|
System.out.println(crcTask.encode());
|
||||||
} else if (args[0].equals("2")) {
|
} else if (args[0].equals("2")) {
|
||||||
|
Loading…
Reference in New Issue
Block a user