Java CRC Calculate crc8(byte data, byte crcInit, byte poly)

Here you can find the source of crc8(byte data, byte crcInit, byte poly)

Description

CRC calculation

License

Open Source License

Parameter

Parameter Description
data The byte to crc check
crcInit The current crc result or another start value
poly The polynom

Return

The crc result

Declaration

public static byte crc8(byte data, byte crcInit, byte poly) 

Method Source Code

//package com.java2s;
/**//from w  ww .  j  av  a  2  s  .  com
 * Copyright (c) 2010-2016, openHAB.org and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

public class Main {
    /**
     * CRC calculation
     * 
     * @param data The byte to crc check
     * @param crcInit The current crc result or another start value
     * @param poly The polynom
     * @return The crc result
     */
    public static byte crc8(byte data, byte crcInit, byte poly) {

        byte crc;
        byte polynom;
        int i;

        crc = crcInit;
        for (i = 0; i < 8; i++) {

            if ((uint(crc) & 0x80) != 0) {
                polynom = poly;
            } else {
                polynom = (byte) 0;
            }

            crc = (byte) ((uint(crc) & ~0x80) << 1);
            if ((uint(data) & 0x80) != 0) {
                crc = (byte) (uint(crc) | 1);
            }

            crc = (byte) (uint(crc) ^ uint(polynom));
            data = (byte) (uint(data) << 1);
        }
        return crc;
    }

    /**
     * Convert to unsigned int
     * 
     * @param v
     * @return
     */
    public static int uint(byte v) {
        return v & 0xFF;
    }
}

Related

  1. crc32(String input)
  2. crc320(byte[] buf, int off, int len)
  3. crc321(byte[] buf, int off, int len)
  4. crc32Hash(String key)
  5. crc64(String value)
  6. crc8(String value)
  7. crc8_tab(byte data, byte crcInit)
  8. crc8PushByte(int[] crc, byte add)
  9. crcUpdate(int old, int value)