static uint8_t crc8( uint8_t * p_buffer, uint16_t buf_size )
{
uint8_t crc = 0;
uint8_t i=0;
if(buf_size <= 0)
{
return crc;
}
while( buf_size-- )
{
for ( i = 0x80; i != 0; i /= 2 )
{
if ( (crc & 0x80) != 0)
{
crc *= 2;
crc ^= 0x07;
}
else
{
crc *= 2;
}
if ( (*p_buffer & i) != 0 )
{
crc ^= 0x07;
}
}
p_buffer++;
}
return crc;
}
#include "stdlib.h"
#include <stdio.h>
typedef unsigned char uint8_t;
typedef unsigned int uint16_t;
int main(void)
{
uint8_t tempbuf[]={0x01,0x02,0x03,0x4,0x5};
uint8_t crc=0;
crc=crc8(tempbuf,5);
printf("crc %x\r\n",crc);
return 0;
}
运行:
root@ubuntu:/home/smbshare/crc8# gcc -o crc crc8.c
root@ubuntu:/home/smbshare/crc8# ./crc
crc bc
对比工具检验
说明接口正确。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有