CRC-16 polinominal 0x1021

Printable View

• 04-13-2011, 04:44 AM
Jerdna
CRC-16 polinominal 0x1021
Please help me.

I want calculate CRC16 for bytes string. C++ algorithm for calculate CRC16:

Code:

```word CRC16(word crc, byte *buf, word len) {   word bits, k;   word accumulator, temp;   for( k = 0; k<len; k++ )   {     accumulator = 0;     temp = (crc>>8)<<8;     for( bits = 0; bits < 8; bits++ )     {       if( (temp ^ accumulator) & 0x8000 )           accumulator = (accumulator << 1) ^ 0x1021;       else           accumulator <<= 1;       temp <<= 1;     }     crc = accumulator^(crc<<8)^(buf[k]&0xff);   }   return crc; }```
I transform into Java:
Code:

```package getbit; import java.io.UnsupportedEncodingException; public class CRC16MassaK {     public static void main(String[] args) throws UnsupportedEncodingException {         int crc = 0xFFFF;          // initial value         int polynomial = 0x1021;  // 0001 0000 0010 0001  (0, 5, 12)         int bits;         int accumulator, temp;         byte[] bytes = {(byte) 0xF8, (byte) 0x55, (byte) 0xCE, 0x05, 0x00, (byte) 0x81, 0x01, 0x00, 0x00, 0x00};         {             for (byte b : bytes) {                 accumulator = 0;                 temp = (crc >> 8) << 8;                 for (bits = 0; bits < 8; bits++) {                     if (((temp ^ accumulator) & 0x8000) == 0x8000)                         accumulator = (accumulator << 1) ^ polynomial;                     else                         accumulator <<= 1;                     temp <<= 1;                 }                 crc = accumulator ^ (crc << 8) ^ (b & 0xFF);             }             System.out.println("CRC16 MassaK = " + Integer.toHexString(crc));         }     } }```
I send bytes string:
Code:

`byte[] bytes = {(byte) 0xF8, (byte) 0x55, (byte) 0xCE, 0x05, 0x00, (byte) 0x81, 0x01, 0x00, 0x00, 0x00};`
And want get two bytes of CRC16:
Code:

`0x5B, 0x3F`
But my Java method return four bytes:
Code:

`0xef, 0xe1, 0x03, 0xc1`
• 09-07-2011, 11:42 AM
Shrinath
Re: CRC-16 polinominal 0x1021
Hi,

I am also facing same problem. Have you find solution for this? If yes, then please let me know..