tags:

views:

104

answers:

1

I've received the task to convert a CRC routine from C to java but I'm really struggling to get the CRC to match up with the documented example. Here's my documentation:

The algorithm used is CCITT-16 (0x1021 or 0x8408 reversed).The remainder is initialized to all 1's (0xFFFF)

WORD crc16_lsb(BYTE *pData, WORD length)
 {
     BYTE i;
     WORD data, crc;

     crc = 0xFFFF;
     if (length == 0)
         return 0;
     do
         {
             data = (WORD)0x00FF & *pData++;
             crc = crc ^ data;
             for (i = 8; i > 0; i--)
                 {
                     if (crc & 0x0001)
                         crc = (crc >> 1) ^ 0x8408;
                     else
                         crc >>= 1;
                 }
         }
     while (--length);
     crc = ~crc;
     return (crc);
 }

As an example, for a Receiver Status Request message made up of the following bytes: 1B FF 41 00

The following 2 byte CRC would be calculated: 27 66

I started with the following javacode from here http://www.cs.princeton.edu/introcs/51data/CRC16CCITT.java.html

Any help would be greatly appreciated.

+1  A: 

Without any closer look, you should know that Java has TWO shift-right operators: ">>" and ">>>". They differ in how the most significant bit is treated, and it may very well be that ">>>" is the most appropriate for CRC-calculations.

Thorbjørn Ravn Andersen