Java code for CRC 16 calculation

6k views Asked by At

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

1

There are 1 answers

4
Mark Adler On BEST ANSWER

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.