当前位置:网站首页>CRC16校验 C语言实现
CRC16校验 C语言实现
2022-07-15 15:23:00 【夏夜晚风_】
循环冗余校验(Cyclic Redundancy Check, CRC)是一种根据网络数据包或计算机文件等数据产生简短固定位数校验码的一种信道编码技术,主要用来检测或校验数据传输或者保存后可能出现的错误。它是利用除法及余数的原理来作错误侦测的。
本文实现 CRC16 的C语言校验算法,采用的是查表的方法。CRC多项式是 x16+x15+x2+1(0x8005),CRC的初始值为0xFFFF,是modbus的CRC校验算法。
1 crc.c
#include "crc16.h"
/* CRC16 余式表 */
static uint16_t crctalbeabs[] = {
0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401,
0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400
};
/*! * 功 能: CRC16校验 * param1: 指向要校验的数据的指针 * param2: 要校验的数据的长度 * retval: 校验所得到的值,uint16_t 类型 * * 说 明: 本次CRC校验为查表法,多项式为 x16+x15+x2+1(0x8005),CRC的初始值为0xFFFF */
uint16_t Crc16(uint8_t *ptr, uint32_t len)
{
uint16_t crc = 0xffff;
uint32_t i;
uint8_t ch;
for (i = 0; i < len; i++) {
ch = *ptr++;
crc = crctalbeabs[(ch ^ crc) & 15] ^ (crc >> 4);
crc = crctalbeabs[((ch >> 4) ^ crc) & 15] ^ (crc >> 4);
}
return crc;
}
2 crc.h
#ifndef _CRC_H_
#define _CRC_H_
#include <stdint.h>
uint16_t Crc16(uint8_t *ptr, uint32_t len);
#define HIG_UINT16(a) ( ((a)>>8) & 0xFF )
#define LOW_UINT16(a) ( (a) & 0xFF )
#define HIG_UINT8(a) ( ((a)>>4) & 0x0F )
#define LOW_UINT8(a) ( (a) & 0x0F )
#endif /* crc.h */
边栏推荐
- E-commerce platform background management system --- > system detailed design (order management module)
- 信息系统项目管理师考前10天极限冲刺+答案(十):综合知识总结
- ERROR 1366 (HY000): Incorrect string value: ‘\xE8\xB5\xB5\xE9\x9B\xB7‘ for column ‘s_name‘ at row 1
- 对接企业微信,客户关系管理也可以很简单!
- (pc+wap) Zhimeng template waterproof building materials website
- Figure 1: four directions and three strategies for digital transformation of state-owned enterprises
- LSM存储模型
- Information system project managers must recite the core examination sites (10) information system planning
- Redis01: introduction to NoSQL and redis
- mysql设置validate_password=off后添加skip-grant-tables服务启动失败
猜你喜欢

Redis05: redis transactions

The 15th day of zero basic Lua --- finally, I'll summarize it

E-commerce platform background management system -- > operation method description

Chess all in one

信息系统项目管理师考前10天极限冲刺+答案(七)

Do you know the debugging skills of the most commonly used Chrome browser console.

Dynamic programming | matrix multiplication

Core examination points for information system project managers (VII) software architecture style

NiFi ListSFTP精讲

动态规划 | 0-1背包问题
随机推荐
Niuke 2021 summer training 3-e-math
创建线程的方式
Kekeguo information management] information management paper writing requirements - unqualified papers
设置圆弧形按钮 设置在正中间居中显示
Niuke 2021 summer training 5-d-double strings
象棋一本通
The distant savior obeys the objective law (III) -- cultural attribute
超强ping工具
分布式事务集成netcore.Cap
Information system project managers must recite the core examination points (I) six elements of the national information system
c# LeetCode刷题笔记1-句子中的最多单词数
Information system project managers must memorize the core examination points (III) 14 graphic tools of UML
3、 Index optimization
In the fiery VR market of yuancosmos, bytes have just touched a little bit
What fault simulation does the chaosblade now support for the database? Do the teachers have any information?
Experience first! What kind of experience is it to write fluent in the browser?
信息系统项目管理师核心考点(九)组织结构类型
uni-app 标签跳转
Redis02: install redis in Linux Environment
牛客2021暑期训练5-D-Double Strings