Java CRC Calculate crc16_get(byte aby[], int nOffset, int nLength)

Here you can find the source of crc16_get(byte aby[], int nOffset, int nLength)

Description

crget

License

Open Source License

Declaration

public static int crc16_get(byte aby[], int nOffset, int nLength) 

Method Source Code

//package com.java2s;
/****************************************************************************
 This file is part of TIImageTool.//from   w w w . j  a v a 2  s .c  o  m

 TIImageTool is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 TIImageTool is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with TIImageTool.  If not, see <http://www.gnu.org/licenses/>.
    
 Copyright 2011 Michael Zapf
 www.mizapf.de
    
 ****************************************************************************/

public class Main {
    public static int crc16_get(byte aby[], int nOffset, int nLength) {
        return crc16_get(aby, nOffset, nLength, 0xffff);
    }

    public static int crc16_get(byte aby[], int nOffset, int nLength,
            int init) {
        // Big-endian, x^16+x^12+x^5+1 = (1) 0001 0000 0010 0001 = 0x1021
        int rem = init;
        int n = 16;
        // A popular variant complements rem here
        int i;
        int j;
        for (i = 0; i < nLength; i++) {
            rem = (rem ^ (aby[i + nOffset] << (n - 8)));
            for (j = 0; j <= 7; j++) {
                if ((rem & 0x8000) == 0x8000) {
                    rem = ((rem << 1) ^ 0x1021);
                } else {
                    rem = (rem << 1);
                }
            }
        }
        // A popular variant complements rem here
        return (rem & 0xffff);
    }
}

Related

  1. computeCRC32(File file)
  2. computeCrc32(InputStream is)
  3. CRC(String message)
  4. crc16(byte[] bytes)
  5. crc16(final byte[] bytes)
  6. CRC16_TABLE(byte[] aData, int aSize)
  7. crc32(byte[] array, int offset, int size)
  8. CRC32(final byte[] buf, final int startPos, final int endPos)
  9. crc32(int[] table, int crc, byte[] buffer, int off, int len)