Results 1 to 2 of 2
  1. #1
    Jerdna's Avatar
    Jerdna is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default CRC-16 polinominal 0x1021

    Please help me.

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

    Java 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:
    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:
    Java Code:
    byte[] bytes = {(byte) 0xF8, (byte) 0x55, (byte) 0xCE, 0x05, 0x00, (byte) 0x81, 0x01, 0x00, 0x00, 0x00};
    And want get two bytes of CRC16:
    Java Code:
    0x5B, 0x3F
    But my Java method return four bytes:
    Java Code:
    0xef, 0xe1, 0x03, 0xc1

  2. #2
    Shrinath is offline Member
    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0

    Default 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..

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •