Computer Interfacing
Discussions about interfacing and electronics
 

CRC-CCITT KERMIT implementation in java (from hex)


 

       Computer Interfacing Forum Index -> Error detection and correction
Author Message
blasipeti
Guest







Mar 19, 2014 3:14 pm

Hi developers!

Somebody can help me in implement this in java?
I found a lot of example for a lot of type of CRC generate, just only for that not. I found from ASCII, but i need generate from hex!
Best regards! Thanks for help!
blasipeti
Guest







Mar 19, 2014 3:27 pm

I tried, that upgrade a simple CRC-CCITT (0xFFFF) to Kermit but it's give a false result... and i dont know why...
Code:

static public int GenerateChecksumCRC16(byte bytes[]) {

        int crc = 0x0000;
        int temp;
        int crc_byte;
        bytes = reverseByteArray(bytes);
       
        for (int i = 0; i < bytes.length; i++) {
            int x = bytes[i];
            byte y = (byte) x;
            bytes[i] = y;
        }

        for (int byte_index = 0; byte_index < bytes.length; byte_index++) {

            crc_byte = bytes[byte_index];

            for (int bit_index = 0; bit_index < 8; bit_index++) {

                temp = ((crc >> 15)) ^ ((crc_byte >> 7));

                crc <<= 1;
                crc &= 0xFFFF;

                if (temp > 0) {
                    crc ^= 0x1021;
                    crc &= 0xFFFF;
                }

                crc_byte <<= 1;
                crc_byte &= 0xFF;

            }
        }
        crc = (int) reverseAndSwapBytes(crc);
        return crc;
    }


i modified the init crc value from 0xffff to 0x0000 and add two plus method
reverseByteArray // this reverse the bits before the process

Code:
 
public static byte[] reverseByteArray(byte[] revByte) {
        byte[] newByte = new byte[revByte.length];
        for (int i = 0, j = revByte.length - 1; i < revByte.length; i++, j--) {
            newByte[i] = (byte) reverseBits(revByte[j], 8);
        }
         return newByte;
    }


and the reverseAndSwapBytes(crc) reverse bits after process and swap the first 2bytes with second 2bytes

Code:

 static int reverseAndSwapBytes(int crc) {
        int crc2 = reverseBits(crc, 16);
        int byte1 = (crc2 & 0xff);
        int byte2 = ((crc2 >> 8) & 0xff);
        int result = ((byte1 << 8) | (byte2));
        return result;
    }
blasipeti
Guest







Mar 19, 2014 4:31 pm

Finally i win! Smile

I found this implementation to c#:
stackoverflow[dot]com/questions/2798374/convert-crc-ccitt-kermit-16-delphi-code-to-c-sharp

and it helped Smile
Here is my working algorithm:


Quote:

static int ComputeCRC(byte[] val) {
int crc;
int q;
byte c;
crc = 0x0000;
for (int i = 0; i < val.length; i++) {
c = val[i];
q = (crc ^ c) & 0x0f;
crc = (crc >> 4) ^ (q * 0x1081);
q = (crc ^ (c >> 4)) & 0x0f;
crc = (crc >> 4) ^ (q * 0x1081);
}
int crcend = ((byte) crc << 8 | (byte) (crc >> 8)) & 0xffff;
//Swap bytes
int byte1 = (crcend & 0xff);
int byte2 = ((crcend >> 8) & 0xff);
int result = ((byte1 << 8) | (byte2));
//Swap
return result;
}

       Computer Interfacing Forum Index -> Error detection and correction
Page 1 of 1



Running on php BB © 2001, 2009 php BB Group
   Lammert Bies     Interfacing     Sitemap     Forum