Java CRC Calculate calculateCRC(byte[] buff, int count)

Here you can find the source of calculateCRC(byte[] buff, int count)

Description

calculate CRC

License

Apache License

Declaration

public static final int calculateCRC(byte[] buff, int count) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static final int calculateCRC(byte[] buff, int count) {

        int crc = 0xFFFF;
        int shifts = 0;
        int flag = 0x0000;

        for (int i = 0; i < buff.length && i < count; i++) {
            // exclusive OR the first 8 bit byte of the message with the low order byte of the 16 bit crc register,
            // putting the result in the crc register.
            crc = (crc & 0xFF00) | ((crc ^ buff[i]) & 0x00FF);
            do {//from w  w w . ja  va  2s . co m
                // shift the crc register one bit to the right, zero-filling the msb. If the LSB was zero,
                // repeat another shift. If the LSB was 1, exclusive XOR the CRC with the value 0xA001;
                flag = crc & 0x0001;
                crc = crc >>> 1;
                if (flag == 0x0001)
                    crc = crc ^ 0xA001;
                shifts++;
                // repeat till 8 shifts have been performed.
            } while (shifts < 8);
        }
        // result is in crc. must swap low and high bytes.
        return ((0x00FF & crc) << 8) | ((0xFF00 & crc) >>> 8);
    }
}

Related

  1. calcCRC(final byte[] data)
  2. calcCRC(int crc, byte[] bytes, int start, int len)
  3. calculateCRC(byte[] data, int offset, int len)
  4. calculateCRC(String digitsToCheck)
  5. calculateCRC(String id)
  6. calculateCrc16(byte[] buffer)