常见CRC算法

CRC算法名称 多项式公式 宽度 简记式 初始值 结果异或值 输入反转 输出反转
CRC-4/ITU x4 + x + 1 4 03 00 00 true true
CRC-5/EPC x5 + x3 + 1 5 09 09 00 false false
CRC-5/ITU x5 + x4 + x2 + 1 5 15 00 00 true true
CRC-5/USB x5 + x2 + 1 5 05 1F 1F true true
CRC-6/ITU x6 + x + 1 6 03 00 00 true true
CRC-7/MMC x7 + x3 + 1 7 09 00 00 false false
CRC-8 x8 + x2 + x + 1 8 07 00 00 false false
CRC-8/ITU x8 + x2 + x + 1 8 07 00 55 false false
CRC-8/ROHC x8 + x2 + x + 1 8 07 FF 00 true true
CRC-8/MAXIM x8 + x5 + x4 + 1 8 31 00 00 true true
CRC-16/IBM x16 + x15 + x2 + 1 16 8005 0000 0000 true true
CRC-16/MAXIM x16 + x15 + x2 + 1 16 8005 0000 FFFF true true
CRC-16/USB x16 + x15 + x2 + 1 16 8005 FFFF FFFF true true
CRC-16/MODBUS x16 + x15 + x2 + 1 16 8005 FFFF 0000 true true
CRC-16/CCITT x16 + x12 + x5 + 1 16 1021 0000 0000 true true
CRC-16/CCITT-FALSE x16 + x12 + x5 + 1 16 1021 FFFF 0000 false false
CRC-16/X25 x16 + x12 + x5 + 1 16 1021 FFFF FFFF true true
CRC-16/XMODEM x16 + x12 + x5 + 1 16 1021 0000 0000 false false
CRC-16/DNP x16 + x13 + x12 + x11 + x10 + x8 + x6 + x5 + x2 + 1 16 3D65 0000 FFFF true true
CRC-32 x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1 32 04C11DB7 FFFFFFFF FFFFFFFF true true
CRC-32/MPEG-2 x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1 32 04C11DB7 FFFFFFFF 00000000 false false

注:
简记式为忽略最高次幂后,多项式幂按位方式表示。
如:CRC-4/ITU多项式x4 + x + 1,其4次幂为1、3次幂为0、2次幂为0、1次幂为1、0次幂为1,忽略最高次幂,安位表示为0011,即简记式为0x03

实现

CRC-16/XMODEM

static const unsigned short crc16tab[256]= {0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef,0x1231,0x0210,0x3273,0x2252,0x52b5,0x4294,0x72f7,0x62d6,0x9339,0x8318,0xb37b,0xa35a,0xd3bd,0xc39c,0xf3ff,0xe3de,0x2462,0x3443,0x0420,0x1401,0x64e6,0x74c7,0x44a4,0x5485,0xa56a,0xb54b,0x8528,0x9509,0xe5ee,0xf5cf,0xc5ac,0xd58d,0x3653,0x2672,0x1611,0x0630,0x76d7,0x66f6,0x5695,0x46b4,0xb75b,0xa77a,0x9719,0x8738,0xf7df,0xe7fe,0xd79d,0xc7bc,0x48c4,0x58e5,0x6886,0x78a7,0x0840,0x1861,0x2802,0x3823,0xc9cc,0xd9ed,0xe98e,0xf9af,0x8948,0x9969,0xa90a,0xb92b,0x5af5,0x4ad4,0x7ab7,0x6a96,0x1a71,0x0a50,0x3a33,0x2a12,0xdbfd,0xcbdc,0xfbbf,0xeb9e,0x9b79,0x8b58,0xbb3b,0xab1a,0x6ca6,0x7c87,0x4ce4,0x5cc5,0x2c22,0x3c03,0x0c60,0x1c41,0xedae,0xfd8f,0xcdec,0xddcd,0xad2a,0xbd0b,0x8d68,0x9d49,0x7e97,0x6eb6,0x5ed5,0x4ef4,0x3e13,0x2e32,0x1e51,0x0e70,0xff9f,0xefbe,0xdfdd,0xcffc,0xbf1b,0xaf3a,0x9f59,0x8f78,0x9188,0x81a9,0xb1ca,0xa1eb,0xd10c,0xc12d,0xf14e,0xe16f,0x1080,0x00a1,0x30c2,0x20e3,0x5004,0x4025,0x7046,0x6067,0x83b9,0x9398,0xa3fb,0xb3da,0xc33d,0xd31c,0xe37f,0xf35e,0x02b1,0x1290,0x22f3,0x32d2,0x4235,0x5214,0x6277,0x7256,0xb5ea,0xa5cb,0x95a8,0x8589,0xf56e,0xe54f,0xd52c,0xc50d,0x34e2,0x24c3,0x14a0,0x0481,0x7466,0x6447,0x5424,0x4405,0xa7db,0xb7fa,0x8799,0x97b8,0xe75f,0xf77e,0xc71d,0xd73c,0x26d3,0x36f2,0x0691,0x16b0,0x6657,0x7676,0x4615,0x5634,0xd94c,0xc96d,0xf90e,0xe92f,0x99c8,0x89e9,0xb98a,0xa9ab,0x5844,0x4865,0x7806,0x6827,0x18c0,0x08e1,0x3882,0x28a3,0xcb7d,0xdb5c,0xeb3f,0xfb1e,0x8bf9,0x9bd8,0xabbb,0xbb9a,0x4a75,0x5a54,0x6a37,0x7a16,0x0af1,0x1ad0,0x2ab3,0x3a92,0xfd2e,0xed0f,0xdd6c,0xcd4d,0xbdaa,0xad8b,0x9de8,0x8dc9,0x7c26,0x6c07,0x5c64,0x4c45,0x3ca2,0x2c83,0x1ce0,0x0cc1,0xef1f,0xff3e,0xcf5d,0xdf7c,0xaf9b,0xbfba,0x8fd9,0x9ff8,0x6e17,0x7e36,0x4e55,0x5e74,0x2e93,0x3eb2,0x0ed1,0x1ef0
};// CRC-16/XMODEM
unsigned short crc16_ccitt_xmodem(const char *buf, int len)
{register int counter;register unsigned short crc = 0;for( counter = 0; counter < len; counter++){crc = (crc << 8) ^ crc16tab[((crc >> 8) ^ *(char *)buf++) & 0x00FF];}return crc;
}
#define ZEXPORT /* empty */
#define cpu_to_le32(x) (x)
#define le32_to_cpu(x) (x)
#define tole(x) cpu_to_le32(x)typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned int uInt;
typedef unsigned char Bytef;
typedef unsigned int size_t;
typedef unsigned int uintptr_t;const unsigned int g_crc_table[256] = {tole(0x00000000L), tole(0x77073096L), tole(0xee0e612cL), tole(0x990951baL),tole(0x076dc419L), tole(0x706af48fL), tole(0xe963a535L), tole(0x9e6495a3L),tole(0x0edb8832L), tole(0x79dcb8a4L), tole(0xe0d5e91eL), tole(0x97d2d988L),tole(0x09b64c2bL), tole(0x7eb17cbdL), tole(0xe7b82d07L), tole(0x90bf1d91L),tole(0x1db71064L), tole(0x6ab020f2L), tole(0xf3b97148L), tole(0x84be41deL),tole(0x1adad47dL), tole(0x6ddde4ebL), tole(0xf4d4b551L), tole(0x83d385c7L),tole(0x136c9856L), tole(0x646ba8c0L), tole(0xfd62f97aL), tole(0x8a65c9ecL),tole(0x14015c4fL), tole(0x63066cd9L), tole(0xfa0f3d63L), tole(0x8d080df5L),tole(0x3b6e20c8L), tole(0x4c69105eL), tole(0xd56041e4L), tole(0xa2677172L),tole(0x3c03e4d1L), tole(0x4b04d447L), tole(0xd20d85fdL), tole(0xa50ab56bL),tole(0x35b5a8faL), tole(0x42b2986cL), tole(0xdbbbc9d6L), tole(0xacbcf940L),tole(0x32d86ce3L), tole(0x45df5c75L), tole(0xdcd60dcfL), tole(0xabd13d59L),tole(0x26d930acL), tole(0x51de003aL), tole(0xc8d75180L), tole(0xbfd06116L),tole(0x21b4f4b5L), tole(0x56b3c423L), tole(0xcfba9599L), tole(0xb8bda50fL),tole(0x2802b89eL), tole(0x5f058808L), tole(0xc60cd9b2L), tole(0xb10be924L),tole(0x2f6f7c87L), tole(0x58684c11L), tole(0xc1611dabL), tole(0xb6662d3dL),tole(0x76dc4190L), tole(0x01db7106L), tole(0x98d220bcL), tole(0xefd5102aL),tole(0x71b18589L), tole(0x06b6b51fL), tole(0x9fbfe4a5L), tole(0xe8b8d433L),tole(0x7807c9a2L), tole(0x0f00f934L), tole(0x9609a88eL), tole(0xe10e9818L),tole(0x7f6a0dbbL), tole(0x086d3d2dL), tole(0x91646c97L), tole(0xe6635c01L),tole(0x6b6b51f4L), tole(0x1c6c6162L), tole(0x856530d8L), tole(0xf262004eL),tole(0x6c0695edL), tole(0x1b01a57bL), tole(0x8208f4c1L), tole(0xf50fc457L),tole(0x65b0d9c6L), tole(0x12b7e950L), tole(0x8bbeb8eaL), tole(0xfcb9887cL),tole(0x62dd1ddfL), tole(0x15da2d49L), tole(0x8cd37cf3L), tole(0xfbd44c65L),tole(0x4db26158L), tole(0x3ab551ceL), tole(0xa3bc0074L), tole(0xd4bb30e2L),tole(0x4adfa541L), tole(0x3dd895d7L), tole(0xa4d1c46dL), tole(0xd3d6f4fbL),tole(0x4369e96aL), tole(0x346ed9fcL), tole(0xad678846L), tole(0xda60b8d0L),tole(0x44042d73L), tole(0x33031de5L), tole(0xaa0a4c5fL), tole(0xdd0d7cc9L),tole(0x5005713cL), tole(0x270241aaL), tole(0xbe0b1010L), tole(0xc90c2086L),tole(0x5768b525L), tole(0x206f85b3L), tole(0xb966d409L), tole(0xce61e49fL),tole(0x5edef90eL), tole(0x29d9c998L), tole(0xb0d09822L), tole(0xc7d7a8b4L),tole(0x59b33d17L), tole(0x2eb40d81L), tole(0xb7bd5c3bL), tole(0xc0ba6cadL),tole(0xedb88320L), tole(0x9abfb3b6L), tole(0x03b6e20cL), tole(0x74b1d29aL),tole(0xead54739L), tole(0x9dd277afL), tole(0x04db2615L), tole(0x73dc1683L),tole(0xe3630b12L), tole(0x94643b84L), tole(0x0d6d6a3eL), tole(0x7a6a5aa8L),tole(0xe40ecf0bL), tole(0x9309ff9dL), tole(0x0a00ae27L), tole(0x7d079eb1L),tole(0xf00f9344L), tole(0x8708a3d2L), tole(0x1e01f268L), tole(0x6906c2feL),tole(0xf762575dL), tole(0x806567cbL), tole(0x196c3671L), tole(0x6e6b06e7L),tole(0xfed41b76L), tole(0x89d32be0L), tole(0x10da7a5aL), tole(0x67dd4accL),tole(0xf9b9df6fL), tole(0x8ebeeff9L), tole(0x17b7be43L), tole(0x60b08ed5L),tole(0xd6d6a3e8L), tole(0xa1d1937eL), tole(0x38d8c2c4L), tole(0x4fdff252L),tole(0xd1bb67f1L), tole(0xa6bc5767L), tole(0x3fb506ddL), tole(0x48b2364bL),tole(0xd80d2bdaL), tole(0xaf0a1b4cL), tole(0x36034af6L), tole(0x41047a60L),tole(0xdf60efc3L), tole(0xa867df55L), tole(0x316e8eefL), tole(0x4669be79L),tole(0xcb61b38cL), tole(0xbc66831aL), tole(0x256fd2a0L), tole(0x5268e236L),tole(0xcc0c7795L), tole(0xbb0b4703L), tole(0x220216b9L), tole(0x5505262fL),tole(0xc5ba3bbeL), tole(0xb2bd0b28L), tole(0x2bb45a92L), tole(0x5cb36a04L),tole(0xc2d7ffa7L), tole(0xb5d0cf31L), tole(0x2cd99e8bL), tole(0x5bdeae1dL),tole(0x9b64c2b0L), tole(0xec63f226L), tole(0x756aa39cL), tole(0x026d930aL),tole(0x9c0906a9L), tole(0xeb0e363fL), tole(0x72076785L), tole(0x05005713L),tole(0x95bf4a82L), tole(0xe2b87a14L), tole(0x7bb12baeL), tole(0x0cb61b38L),tole(0x92d28e9bL), tole(0xe5d5be0dL), tole(0x7cdcefb7L), tole(0x0bdbdf21L),tole(0x86d3d2d4L), tole(0xf1d4e242L), tole(0x68ddb3f8L), tole(0x1fda836eL),tole(0x81be16cdL), tole(0xf6b9265bL), tole(0x6fb077e1L), tole(0x18b74777L),tole(0x88085ae6L), tole(0xff0f6a70L), tole(0x66063bcaL), tole(0x11010b5cL),tole(0x8f659effL), tole(0xf862ae69L), tole(0x616bffd3L), tole(0x166ccf45L),tole(0xa00ae278L), tole(0xd70dd2eeL), tole(0x4e048354L), tole(0x3903b3c2L),tole(0xa7672661L), tole(0xd06016f7L), tole(0x4969474dL), tole(0x3e6e77dbL),tole(0xaed16a4aL), tole(0xd9d65adcL), tole(0x40df0b66L), tole(0x37d83bf0L),tole(0xa9bcae53L), tole(0xdebb9ec5L), tole(0x47b2cf7fL), tole(0x30b5ffe9L),tole(0xbdbdf21cL), tole(0xcabac28aL), tole(0x53b39330L), tole(0x24b4a3a6L),tole(0xbad03605L), tole(0xcdd70693L), tole(0x54de5729L), tole(0x23d967bfL),tole(0xb3667a2eL), tole(0xc4614ab8L), tole(0x5d681b02L), tole(0x2a6f2b94L),tole(0xb40bbe37L), tole(0xc30c8ea1L), tole(0x5a05df1bL), tole(0x2d02ef8dL)
};#define do_crc(x, tab, crc) ((crc) = (tab)[((crc) ^ (x)) & 255] ^ ((crc) >> 8))unsigned int crc32_no_comp(unsigned int crc, const Bytef *buf, uInt len)
{const unsigned int *tab = g_crc_table;const unsigned int *b = (const unsigned int *)buf;size_t rem_len;/* Align it */uintptr_t temp_b = (uintptr_t)b;if ((((temp_b) & 3) != 0) && (len != 0)) { /* 3:low 2bits */uint8_t *p = (uint8_t *)b;uintptr_t temp_p = (uintptr_t)p;do {do_crc(*p++, tab, crc);} while (((--len) != 0) && (((temp_p) & 3) != 0)); /* 3:low 2bits */b = (unsigned int *)p;}rem_len = len & 3; /* 3:low 2bits */len = len >> 2;    /* high 2bits */for (--b; len != 0; --len) {/* load data 32 bits wide, xor data 32 bits wide. */crc ^= *++b; /* use pre increment for speed */do_crc(0, tab, crc);do_crc(0, tab, crc);do_crc(0, tab, crc);do_crc(0, tab, crc);}len = rem_len;/* And the last few bytes */if (len != 0) {uint8_t *p = (uint8_t *)(b + 1) - 1;do {do_crc(*++p, tab, crc); /* use pre increment for speed */} while ((--len) != 0);}return le32_to_cpu(crc);
}
#undef do_crcunsigned int crc32(unsigned int crc, const Bytef *p, uInt len)
{return crc32_no_comp(crc ^ 0xffffffffL, p, len) ^ 0xffffffffL;
}

验证网站

http://www.ip33.com/crc.html

常见CRC算法与C实现相关推荐

  1. 完整性校验用到常见的算法_几种常见的校验算法

    素材来源:网络 编辑整理:strongerHuang UART有一个奇偶校验,CAN通信有CRC校验.Modbus.MAVlink.USB等通信协议也有校验信息. 在自定义数据存储时,有经验的工程师都 ...

  2. tcp中的crc检验算法原理_在数据传输过程中的CRC 算法的简单说明

    CRC校验(循环冗余校验)是数据通讯中最常采用的校验方式.在嵌入式软件开发中,经常要用到CRC 算法对各种数据进行校验.因此,掌握基本的CRC算法应是嵌入式程序员的基本技能.可是,我认识的嵌入式程序员 ...

  3. 纵向冗余校验计算方法_常见校验算法

    常见校验算法 一.校验算法 奇偶校验 MD5 校验 求校验和 BCC(Block Check Character/ 信息组校验码 ) ,好像也是常说的异或校验方法 CRC(Cyclic Redunda ...

  4. 常见安全算法(RSA、AES、MD5等)原理及实现和win10下OpenSSL库的使用

    一.安全算法概述 安全算法通常被人们分为对称加密算法和非对称加密算法两类. 对称加密算法是指加密秘钥和解密秘钥相同的算法,即用什么加密就用什么解密,早期的安全算法均属于这一类.目前常见的对称加密算法有 ...

  5. CRC 算法的简单说明

    写给嵌入式程序员的循环冗余校验(CRC)算法入门引导 前言 CRC校验(循环冗余校验)是数据通讯中最常采用的校验方式.在嵌入式软件开发中,经常要用到CRC 算法对各种数据进行校验.因此,掌握基本的CR ...

  6. 循环冗余校验CRC算法

    本文参考自: https://blog.csdn.net/liyuanbhu/article/details/7882789#commentsedit 博主的文章写的太好了,我这里仅仅摘抄几个重要的知 ...

  7. php常见排序算去,PHP兑现常见排序算法

    PHP实现常见排序算法 //插入排序(一维数组) function insert_sort($arr){ $count = count($arr); for($i=1; $i $tmp = $arr[ ...

  8. Java常见排序算法

    Java常见排序算法 转载于:https://www.cnblogs.com/hfultrastrong/p/7829889.html

  9. 8种常见机器学习算法比较

    8种常见机器学习算法比较 2016-08-04 17:46 转载 陈圳 0条评论 雷锋网(搜索"雷锋网"公众号关注)按:本文转自刘志伟责编,在机器学习中选择一个恰当的算法十分重要, ...

  10. moead算法流程步骤_数据聚类(一)常见聚类算法的基本原理[图解]

    文章整理了五种常见聚类算法的基本原理,通过简易图解的形式对算法原理进行形象化的描述,同时给出了算法的实现流程和数学表达.全文约4192字. 相关名词的英文翻译 监督学习Supervised Learn ...

最新文章

  1. ubuntu笔记:查看Ubuntu的包依赖关系
  2. c语言中说取消标识符是,2019年全国计算机二级C语言考试考点解析(3)
  3. addcslashes php 有什么用处,PHP addcslashes函数有什么用
  4. Java中的String、StringBuffer、StringBuilder的区别和使用范围
  5. Python机器学习算法 — 逻辑回归(Logistic Regression)
  6. SDUT - 2604 Thrall’s Dream(tarjan+拓扑)
  7. 在sp_executesql中使用like字句
  8. C++——面向对象设计原则
  9. linux卸载apk命令,apk的安装和卸载 - Jenly的个人空间 - OSCHINA - 中文开源技术交流社区...
  10. 编程实现激光雷达点云数据提取道路特征
  11. 孙正义:一个有远见的赌徒
  12. 《工业设计史》第八章:20世纪20、30年代的流行风格
  13. origin 修改默认字体
  14. Pyramid Vision Transformer: A Versatile Backbone for Dense Prediction without Convolutions
  15. 主流低功耗服务器u,新组低功耗NAS服务器(1037U)分享
  16. 计算机网络自顶向下方法(第六版) 课后题答案 | 第五章
  17. GIS的基本概念二:大地水准面、旋转椭球体(椭球体)、大地基准面
  18. 什么是Anti-DDoS流量清洗?
  19. [42000][1064] You have an error in your SQL syntax; IDEA连接数据库测SQL时报异常
  20. 融合知识图谱和用户行为信息的个性化推荐算法研究

热门文章

  1. 【编程马拉松】【014-红与黑】
  2. Spring Security--基于注解访问控制 @Secured@PreAuthorize
  3. mysql 定时调用sp_使用shell脚本调用mysql数据库存储过程,并设置定时任务
  4. Matlab 多层(multi-level)小波分析(dwt,dwt2)
  5. 404页面是什么意思?怎么正确设置?
  6. 2 PC 有它,你就够了!
  7. HTML学生个人网站作业设计:动漫网站设计——悬崖上的金鱼姬(5页) HTML+CSS 简单DIV布局网页模板代码
  8. 图像分类经典卷积神经网络—SENet论文翻译(纯中文版)—Squeeze-and-Excitation Networks(挤压和激励网络)
  9. What Is 'FTW'? What Does It Mean?
  10. 基于FPGA的ADS1256讲解