Java CRC Calculate calcCRC(final byte[] data)

Here you can find the source of calcCRC(final byte[] data)

Description

Calculates the CRC over all config data packets (including the last with the null bytes at the end) (see page 49)

License

Open Source License

Declaration

public static int calcCRC(final byte[] data) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from www. ja  va 2s  .  com*/
     * Calculates the CRC over all config data packets (including the last with the null bytes at the end)
     * (see page 49)
     */
    public static int calcCRC(final byte[] data) {
        final int POLY = 0x1021;
        final int START_VALUE = 0xFFFF;

        int crc = START_VALUE;

        for (int i = 0; i < data.length; i++) {

            crc = crc ^ ((data[i] & 0xFF) << 8);

            for (int j = 0; j < 8; j++) {
                if ((crc & 0x8000) == 0x8000) {
                    crc = crc << 1;
                    crc = crc ^ POLY;
                } else {
                    crc = crc << 1;
                }
            }
        }

        return crc & 0xFFFF;
    }
}

Related

  1. calcCRC(int crc, byte[] bytes, int start, int len)
  2. calculateCRC(byte[] buff, int count)
  3. calculateCRC(byte[] data, int offset, int len)
  4. calculateCRC(String digitsToCheck)