0

Below is my hex string

String str="01312D0102D6010162000600000918000000000000000000000000007FFF00150010004B0100003A200207000004040055516200050E9E000200000000005852324531533156304330000001614B00541E0C07110507320000000000000000000000000500000000000000000406000030313030313033300000000000000000000046300A1D1655000186A0FFFF9EAA00000043000000000200000C000C000C000C000D000C000D000C000D000C000D000D000C000C000C000C000D000C000D000D000C000D000D000C000D000D000C000D000D000D000C000D000D000D000E000D000C000D000D000D000D000C000D000D000D000E000D000D000D000C000D000D029E029F02F102EA05300544085A07C1060F040A036404E0072F064804AA040404070510066B067205C304B302C6000D000D000D000E000D000D000E000D000E000D000D000E000D000D000E000D000E000D000E000D000E000D000D0065006300810081025102E001990082001F000E000D000E000D000E000E000D000E000D000E000E001500560062003300480068009000AF00AE00AE0214048404670560007E000E000E000E000E000E000F000E000E000E000E000E005501290168000E000E000D000E000E000E000D000E000E000E000D000E000E000D000E000E000D000E000E000D000E000E000D000E000D000E000E000E000D000E000E000D000E000E000D000E000E000D000E000E000E000D000E000E000D000E000E000D000E000E000E000D000E000E000E000D000E000E000E000D000E000E000D000E000E000E000D000E000E000E000D000E000E000E000D000E000E000E000D000E000E000E000D000E000E000D000E000E000D000E000E000E000D000E000D000E000D000E000D000E000D000E000E000D000E000D000D000D000D000D000C000D000D000E000D000D000D000D000D000D000D000E000D000D000E000D000D000E000D000D000D000D000D00E200DF00DF000F000E000D007300DF";

and this is my CRC : A228 which is appended to the above string. I want output as A228

Polynomial used is 0xA001

Can someone provide me the code to calculate the CRC16

Below is my java code

String str="01312D0102D6010162000600000918000000000000000000000000007FFF00150010004B0100003A200207000004040055516200050E9E000200000000005852324531533156304330000001614B00541E0C07110507320000000000000000000000000500000000000000000406000030313030313033300000000000000000000046300A1D1655000186A0FFFF9EAA00000043000000000200000C000C000C000C000D000C000D000C000D000C000D000D000C000C000C000C000D000C000D000D000C000D000D000C000D000D000C000D000D000D000C000D000D000D000E000D000C000D000D000D000D000C000D000D000D000E000D000D000D000C000D000D029E029F02F102EA05300544085A07C1060F040A036404E0072F064804AA040404070510066B067205C304B302C6000D000D000D000E000D000D000E000D000E000D000D000E000D000D000E000D000E000D000E000D000E000D000D0065006300810081025102E001990082001F000E000D000E000D000E000E000D000E000D000E000E001500560062003300480068009000AF00AE00AE0214048404670560007E000E000E000E000E000E000F000E000E000E000E000E005501290168000E000E000D000E000E000E000D000E000E000E000D000E000E000D000E000E000D000E000E000D000E000E000D000E000D000E000E000E000D000E000E000D000E000E000D000E000E000D000E000E000E000D000E000E000D000E000E000D000E000E000E000D000E000E000E000D000E000E000E000D000E000E000D000E000E000E000D000E000E000E000D000E000E000E000D000E000E000E000D000E000E000E000D000E000E000D000E000E000D000E000E000E000D000E000D000E000D000E000D000E000D000E000E000D000E000D000D000D000D000D000C000D000D000E000D000D000D000D000D000D000D000E000D000D000E000D000D000E000D000D000D000D000D00E200DF00DF000F000E000D007300DF";//A228";

    byte arr[] = toByteArray(str);


        long polynomial = 0xA001;
        long CRC = 0xFFFF;

        for (byte b : arr)
        {
            CRC ^= b;
            for (int i = 8; i != 0; i--)
            {
                if ((CRC & 0x0001) != 0)
                {
                    CRC = (CRC >> 1) ^ polynomial;
                }
                else
                {
                    CRC >>= 1;
                }
            }
        }



        System.out.println(CRC);

}

I am getting output as -56289

Faiz
  • 230
  • 4
  • 20

1 Answers1

3

Your code is mostly fine and will produce 0xa228 for that message (with the modification below), except you have to feed it the right data and then display the resulting CRC correctly.

To feed it the right data, you first need to convert that string of hexadecimal characters into half that many binary bytes. Give that to your CRC routine.

Second, display the result in hexadecimal, not decimal.

Third, byte is signed, so do CRC ^= b & 0xff; to avoid the sign extension.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
  • I am converting the string into byte array. Also I am printing it into hex and still I am getting output as : ffffffffffff3637 – Faiz Sep 13 '17 at 05:27
  • Read my answer more carefully. You need to convert the _hexadecimal_ into bytes. Your 1460 characters would become 730 bytes, not 1460 bytes. – Mark Adler Sep 13 '17 at 16:24
  • Also I noticed that you need to fix the signed byte problem in Java. – Mark Adler Sep 13 '17 at 16:43
  • 1
    You need to provide your `toByteArray` routine to know what's really going on here. – Mark Adler Sep 13 '17 at 16:49