package com.ld.igds.protocol.bhzn.utils; public class CRC { /*寄存器初始值*/ private final int regInitValue; private int register; /*CRC 多项式 modbus 多项式0x8005*/ private final int polynomial; public CRC() { this(0xffff, 0x8005); } public CRC(int register, int polynomial) { this.register = register; this.regInitValue = register; this.polynomial = Integer.reverse(polynomial)>>>Integer.numberOfLeadingZeros(polynomial); } private void byteCRC(byte byt) { register ^= (byt&0xff); for(int i=0; i<8; i++){ if((register&1)==0){ register >>= 1; }else{ register = (register >> 1)^polynomial; } } } public synchronized int crc(byte[] byts) { if(null==byts||byts.length==0){ return 0; } for(int i=0; i