Java CRC Calculate calcCRC(int crc, byte[] bytes, int start, int len)

Here you can find the source of calcCRC(int crc, byte[] bytes, int start, int len)

Description

calc CRC

License

Open Source License

Declaration

public static int calcCRC(int crc, byte[] bytes, int start, int len) 

Method Source Code

//package com.java2s;

public class Main {
    private static int[] _cache;

    public static int calcCRC(int crc, byte[] bytes, int start, int len) {
        initCRCCache();//from  w  w w  . j  a  v a 2s.c o m
        for (int j = 0; j < len; ++j) {
            final byte b = bytes[j + start];
            crc = calcCRC(crc, b);
        }
        return crc;
    }

    public static int calcCRC(int crc, byte b) {
        int index = crc >>> 24;
        index ^= ((int) b) & 0xff;
        crc <<= 8;
        crc ^= _cache[index];
        return crc;
    }

    private static void initCRCCache() {
        if (_cache != null)
            return;
        _cache = new int[256];
        for (int j = 0; j < 256; ++j) {
            int value = j << 24;
            for (int bit = 0; bit < 8; ++bit) {
                if ((value & 0x80000000) == 0x80000000) { // bit31 is set
                    value <<= 1;
                    value ^= 0xAF;
                } else {
                    value <<= 1;
                }
            }
            value &= 0xffff;
            _cache[j] = value;
        }
    }
}

Related

  1. calcCRC(final byte[] data)
  2. calculateCRC(byte[] buff, int count)
  3. calculateCRC(byte[] data, int offset, int len)
  4. calculateCRC(String digitsToCheck)
  5. calculateCRC(String id)