首页 > 其他分享 >CRC16

CRC16

时间:2024-10-25 14:49:22浏览次数:1  
标签:0xC1 CRC16 0x00 0x01 0x41 0x80 byte

CRC16 算法

循环冗余算法

/**
 * 循环冗余校验算法
 *
 * 代码中的常量,仅用于提示相关算法,无实际功能
 *
 * @author Mr.css
 * @version 2024-10-22 10:49
 */
public class CRC16Utils {


    /**
     * 名称:CRC4 多项式:x4+x+1 简记式:3
     */
    public static final int CRC4 = 0x3;
    /**
     * 名称:CRC8 多项式:x8+x5+x4+1 简记式:0x31
     */
    public static final int CRC8_1 = 0x31;
    /**
     * 名称:CRC8 多项式:x8+x2+x1+1 简记式:0x07
     */
    public static final int CRC8_2 = 0x07;
    /**
     * 名称:CRC8 多项式:x8+x6+x4+x3+x2+x1 简记式:0x5E
     */
    public static final int CRC8_3 = 0x5E;
    /**
     * 名称:CRC-12 多项式:x12+x11+x3+x+1 简记式:0x80F
     */
    public static final int CRC12 = 0x80F;
    /**
     * 名称:CRC16-CCITT 多项式:x16+x12+x5+1 简记式:1021
     */
    public static final int CRC16_CCITT = 0x1021;
    /**
     * 名称:CRC16 多项式:x16+x15+x2+1 简记式:8005
     * 0xA001 是 0x8005 的按位颠倒形式,两个都可以实现;
     * MODBUS 的初始值是 0xFFFF,结果与 0x0000 进行异或运算
     * <p>
     * 相同的多项式,通过调整初始值和结果值,可以产生 4 种不同的效果:
     * 分别是:CRC16_MODBUS、CRC16_IBM、CRC16_MAXIM、CRC16_USB
     */
    public static final int CRC16_MODBUS = 0xA001;
    /**
     * 名称:CRC32 多项式:x32+x26+x23+...+x2+x+1 简记式:04C11DB7
     * 用在以太网中
     */
    public static final int CRC32 = 0x04C11DB7;
    /**
     * 名称:CRC32c 多项式:x32+x28+x27+...+x8+x6+1简记式:1EDC6F41
     */
    public static final int CRC32c = 0x1EDC6F41;


    /**
     * CRC16_MODBUS 查找表
     * <p>
     * 在 CRC16 校验中,aucCRCHi和aucCRCLo是根据 CRC16 多项式和初始值生成,
     * 这两个表包含了 256 种可能的CRC值,每个值对应一个8位的输入数据与初始CRC寄存器的高位或低位进行异或操作后的结果。
     * 这些数据提前算好存入内存,会比临时计算效率更高‌。
     */
    private static final short[] aucCRCHi = {
            (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40,
            (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,
            (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,
            (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40,
            (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,
            (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40,
            (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40,
            (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,
            (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,
            (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40,
            (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40,
            (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,
            (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40,
            (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,
            (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,
            (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40
    };

    private static final byte[] aucCRCLo = {
            (byte) 0x00, (byte) 0xC0, (byte) 0xC1, (byte) 0x01, (byte) 0xC3, (byte) 0x03, (byte) 0x02, (byte) 0xC2, (byte) 0xC6, (byte) 0x06, (byte) 0x07, (byte) 0xC7, (byte) 0x05, (byte) 0xC5, (byte) 0xC4, (byte) 0x04,
            (byte) 0xCC, (byte) 0x0C, (byte) 0x0D, (byte) 0xCD, (byte) 0x0F, (byte) 0xCF, (byte) 0xCE, (byte) 0x0E, (byte) 0x0A, (byte) 0xCA, (byte) 0xCB, (byte) 0x0B, (byte) 0xC9, (byte) 0x09, (byte) 0x08, (byte) 0xC8,
            (byte) 0xD8, (byte) 0x18, (byte) 0x19, (byte) 0xD9, (byte) 0x1B, (byte) 0xDB, (byte) 0xDA, (byte) 0x1A, (byte) 0x1E, (byte) 0xDE, (byte) 0xDF, (byte) 0x1F, (byte) 0xDD, (byte) 0x1D, (byte) 0x1C, (byte) 0xDC,
            (byte) 0x14, (byte) 0xD4, (byte) 0xD5, (byte) 0x15, (byte) 0xD7, (byte) 0x17, (byte) 0x16, (byte) 0xD6, (byte) 0xD2, (byte) 0x12, (byte) 0x13, (byte) 0xD3, (byte) 0x11, (byte) 0xD1, (byte) 0xD0, (byte) 0x10,
            (byte) 0xF0, (byte) 0x30, (byte) 0x31, (byte) 0xF1, (byte) 0x33, (byte) 0xF3, (byte) 0xF2, (byte) 0x32, (byte) 0x36, (byte) 0xF6, (byte) 0xF7, (byte) 0x37, (byte) 0xF5, (byte) 0x35, (byte) 0x34, (byte) 0xF4,
            (byte) 0x3C, (byte) 0xFC, (byte) 0xFD, (byte) 0x3D, (byte) 0xFF, (byte) 0x3F, (byte) 0x3E, (byte) 0xFE, (byte) 0xFA, (byte) 0x3A, (byte) 0x3B, (byte) 0xFB, (byte) 0x39, (byte) 0xF9, (byte) 0xF8, (byte) 0x38,
            (byte) 0x28, (byte) 0xE8, (byte) 0xE9, (byte) 0x29, (byte) 0xEB, (byte) 0x2B, (byte) 0x2A, (byte) 0xEA, (byte) 0xEE, (byte) 0x2E, (byte) 0x2F, (byte) 0xEF, (byte) 0x2D, (byte) 0xED, (byte) 0xEC, (byte) 0x2C,
            (byte) 0xE4, (byte) 0x24, (byte) 0x25, (byte) 0xE5, (byte) 0x27, (byte) 0xE7, (byte) 0xE6, (byte) 0x26, (byte) 0x22, (byte) 0xE2, (byte) 0xE3, (byte) 0x23, (byte) 0xE1, (byte) 0x21, (byte) 0x20, (byte) 0xE0,
            (byte) 0xA0, (byte) 0x60, (byte) 0x61, (byte) 0xA1, (byte) 0x63, (byte) 0xA3, (byte) 0xA2, (byte) 0x62, (byte) 0x66, (byte) 0xA6, (byte) 0xA7, (byte) 0x67, (byte) 0xA5, (byte) 0x65, (byte) 0x64, (byte) 0xA4,
            (byte) 0x6C, (byte) 0xAC, (byte) 0xAD, (byte) 0x6D, (byte) 0xAF, (byte) 0x6F, (byte) 0x6E, (byte) 0xAE, (byte) 0xAA, (byte) 0x6A, (byte) 0x6B, (byte) 0xAB, (byte) 0x69, (byte) 0xA9, (byte) 0xA8, (byte) 0x68,
            (byte) 0x78, (byte) 0xB8, (byte) 0xB9, (byte) 0x79, (byte) 0xBB, (byte) 0x7B, (byte) 0x7A, (byte) 0xBA, (byte) 0xBE, (byte) 0x7E, (byte) 0x7F, (byte) 0xBF, (byte) 0x7D, (byte) 0xBD, (byte) 0xBC, (byte) 0x7C,
            (byte) 0xB4, (byte) 0x74, (byte) 0x75, (byte) 0xB5, (byte) 0x77, (byte) 0xB7, (byte) 0xB6, (byte) 0x76, (byte) 0x72, (byte) 0xB2, (byte) 0xB3, (byte) 0x73, (byte) 0xB1, (byte) 0x71, (byte) 0x70, (byte) 0xB0,
            (byte) 0x50, (byte) 0x90, (byte) 0x91, (byte) 0x51, (byte) 0x93, (byte) 0x53, (byte) 0x52, (byte) 0x92, (byte) 0x96, (byte) 0x56, (byte) 0x57, (byte) 0x97, (byte) 0x55, (byte) 0x95, (byte) 0x94, (byte) 0x54,
            (byte) 0x9C, (byte) 0x5C, (byte) 0x5D, (byte) 0x9D, (byte) 0x5F, (byte) 0x9F, (byte) 0x9E, (byte) 0x5E, (byte) 0x5A, (byte) 0x9A, (byte) 0x9B, (byte) 0x5B, (byte) 0x99, (byte) 0x59, (byte) 0x58, (byte) 0x98,
            (byte) 0x88, (byte) 0x48, (byte) 0x49, (byte) 0x89, (byte) 0x4B, (byte) 0x8B, (byte) 0x8A, (byte) 0x4A, (byte) 0x4E, (byte) 0x8E, (byte) 0x8F, (byte) 0x4F, (byte) 0x8D, (byte) 0x4D, (byte) 0x4C, (byte) 0x8C,
            (byte) 0x44, (byte) 0x84, (byte) 0x85, (byte) 0x45, (byte) 0x87, (byte) 0x47, (byte) 0x46, (byte) 0x86, (byte) 0x82, (byte) 0x42, (byte) 0x43, (byte) 0x83, (byte) 0x41, (byte) 0x81, (byte) 0x80, (byte) 0x40
    };

    /**
     * CRC16-MODBUS
     *
     * 因为 byte 是有符号的,没办法表示无符号的 16 进制数,强制转换之后会变成负数,
     * 位运算没问题,不影响当前函数的运行,需要注意四则运算。
     *
     * @param data 数据
     * @return 校验位
     */
    public static int useCRC16MB(byte[] data) {
        int ucCRCHi = 0x00ff;
        int ucCRCLo = 0x00ff;
        int iIndex;
        for (byte d : data) {
            iIndex = (ucCRCLo ^ d) & 0x00ff;
            ucCRCLo = ucCRCHi ^ aucCRCHi[iIndex];
            ucCRCHi = aucCRCLo[iIndex];
        }

        return (ucCRCHi << 8) | (ucCRCLo & 0xFF);
    }

    /**
     * 高低位互换
     */
    public int reverse(int crc) {
        return ((crc & 0xFF00) >> 8) | ((crc & 0x00FF) << 8);
    }

    /**
     * CRC16-MODBUS
     * <p>
     * 算法与上面完全一致,只是没用查找表,效率相对低一些
     *
     * @param bytes -
     * @return -
     */
    public static int userCRC16MB2(byte[] bytes) {
        int crc = 0xffff;
        for (byte b : bytes) {
            crc ^= (b & 0xff);
            for (int j = 0; j < 8; j++) {
                if ((crc & 0x0001) != 0) {
                    crc = ((crc >>> 1) ^ CRC16_MODBUS);
                } else {
                    crc = crc >>> 1;
                }
            }
        }
        return crc;
    }

    // 主函数,用于测试
    public static void main(String[] args) {
        byte[] data = {
            (byte) 0x78, (byte) 0x76, (byte) 0xD0, (byte) 0xB9, (byte) 0x01, (byte) 0x66, (byte) 0xAF, (byte) 0x44, (byte) 0x41, (byte) 0xCC, (byte) 0x12, (byte) 0x29, (byte) 0x41, (byte) 0x58, (byte) 0x3E, (byte) 0xE2, (byte) 0x41, (byte) 0x4F, (byte) 0x15, (byte) 0x3C, (byte) 0x42, (byte) 0xCD, (byte) 0xCC, (byte) 0xD0, (byte) 0x41,
            (byte) 0x3D, (byte) 0x8A, (byte) 0xEE, (byte) 0x42, (byte) 0x1F, (byte) 0x38, (byte) 0x39, (byte) 0x38, (byte) 0x36, (byte) 0x30, (byte) 0x34, (byte) 0x36, (byte) 0x38, (byte) 0x31, (byte) 0x35, (byte) 0x32, (byte) 0x30, (byte) 0x43, (byte) 0x31, (byte) 0x33, (byte) 0x32, (byte) 0x35, (byte) 0x39, (byte) 0x33, (byte) 0x34,
            (byte) 0x02, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x70, (byte) 0x8C, (byte) 0x44, (byte) 0x03, (byte) 0x00, (byte) 0xCD, (byte) 0x8C, (byte) 0x1E, (byte) 0x45, (byte) 0x01, (byte) 0x02, (byte) 0x14, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x08, (byte) 0xFA, (byte) 0xC1,
            (byte) 0x5B, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x04, (byte) 0x0F, (byte) 0x60, (byte) 0x04, (byte) 0x9A, (byte) 0x99, (byte) 0x49, (byte) 0x41, (byte) 0xCD, (byte) 0xCC, (byte) 0x7C, (byte) 0x41, (byte) 0x9A, (byte) 0x99, (byte) 0x49, (byte) 0x41, (byte) 0x33, (byte) 0x33, (byte) 0x97,
            (byte) 0x41
        };

        // 示例数据
        // 0x12, 0x3E
        System.out.printf("CRC16 Value: 0x%04X", userCRC16MB2(data));
        System.out.println();
        System.out.println();
        System.out.printf("CRC16 Value: 0x%04X", useCRC16MB(data));
    }
}

C语言实现

这个是原始算法,如果对上面算法存疑,可以看看。

上面的算法由文言一心翻译而来(后又经过代码精简)。

uint16_t usMBCRC16(uint8_t * pucFrame, uint16_t usLen)
{
	 uint8_t ucCRCHi = 0xFF;
	 uint8_t ucCRCLo = 0xFF;
	 int iIndex;
	 while(usLen--)
	 {
	 iIndex = ucCRCLo ^ *(pucFrame++);
	 ucCRCLo = (uint8_t)(ucCRCHi ^ aucCRCHi[iIndex]);
	 ucCRCHi = aucCRCLo[iIndex];
	 }
	 return (uint16_t)(ucCRCHi << 8 | ucCRCLo);
}

标签:0xC1,CRC16,0x00,0x01,0x41,0x80,byte
From: https://www.cnblogs.com/chenss15060100790/p/18502538

相关文章

  • [转]相同CRC不同数据的测试.CRC16 - CRC64 test results on 18.2M dataset
    转载自: http://www.backplane.com/matt/crc64.html  CRC16-CRC64testresultson18.2Mdataset,w/programsourceProgram&TestRunbyMattDillon18.2Mmessage-iddatasetsuppliedbyJoeGrecoIwouldliketothankeveryonewhoofferedtheirhistoryf......
  • crc16 linux版本
    #include<stdio.h>#include<stdint.h>//CRC-16-CCITT标准的多项式#definePOLY0x1021uint16_tcrc16(uint16_tcrc,constunsignedchar*buffer,size_tlen){while(len--){crc^=(*buffer++<<8);for(inti=0;i<......
  • crc16计算结果相同
    通过这个网址  CRC(循环冗余校验)在线计算_ip33.com 验证在crc16/XMODEM模式下不同的输入会有相同的输出,下例的结果都是0x62d30003003ca55a101800000000000007e21a881aa61aa31a9f1a971a951a9a1a8c1c45a55a001800000000000007e31......
  • modbus crc16校验
    //计算crc16位校验,如计算结果是0x5566,则高字节是55,低字节是66,需要根据情况交换高低字节quint16checkCrc16(quint8*ptr,intlen){unsignedinti;quint16crc=0xFFFF;while(len--){crc^=*ptr++;for(i=0;i<8;++i)......
  • 使用C#配合modbus协议的16进制代码生成crc16校验码的计算方法
    前言在网上也是查看了很多关于crc16校验的文章,但是好像都是对于有基础的人看的,我当时拿起直接使用,发现行不通,这对于零基础的不是很友好,所以决定贡献一篇,哈哈哈哈~~~publicuintCalcCRC16(stringhexCommand){byte[]pBuf=HexStringToByteArray(......
  • Java计算CRC16校验码
    废话不多说,直接上代码/***计算CRC16校验码**@parambytes需要计算的字节数组*/publicstaticbyte[]getCRCByteArray(byte[]bytes){//ModBus通信协议的CRC(冗余循环校验码含2个字节,即16位二进制数。//......
  • crc16校验C语言源码实例解析
    一概念:循环冗余码校验英文名称为CyclicalRedundancyCheck,简称CRC。它是利用除法及余数的原理来作错误侦测(ErrorDetecting)的。实际应用时,发送装置计算出CRC值并随数据一同发送给接收装置,接收装置对收到的数据重新计算CRC并与收到的CRC相比较,若两个CRC值不同,则说明数据通讯出现......
  • 使用C++实现Modbus CRC16检验相关内容
    使用C++实现ModbusCRC16检验相关内容ModbusCRC-16校验代码以下为ModbusCRC-16校验代码函数:其中参数int*crc_sum为校验返回值,分为两个字节;参数int*data_blk_ptr......
  • C# CRC16计算校验域
    ///<summary>///计算校验域(CRC16)///</summary>///<paramname="data"></param>///<returns></returns>publicstaticbyte[......
  • CRC16码
    1.预置1个16位的寄存器为十六进制FFFF(即全为1);称此寄存器为CRC寄存器;2.把第一个8位二进制数据(既通讯信息帧的第一个字节)与16位的CRC寄存器的低8位相异或,把结果放于CRC寄存器;3.......