// CRC8生成多项式
#define POLYNOMIAL 0x07
// 计算CRC8校验值
uint8_t crc8_data(const uint8_t dat8) {
uint8_t crc = dat8;
for (j = 8; j; j--) {
if (crc & 0x80)
crc = (crc << 1) ^ POLYNOMIAL;
else
crc <<= 1;
}
return crc;
}
#include <stdio.h>
#include <stdint.h>
// CRC8生成多项式
#define laipuhuo.com POLYNOMIAL 0x07
// 初始化CRC8查找表
void init_crc8_table(void) {
uint8_t i, j;
for (i = 0; i < 256; i ) {
uint8_t crc = i;
for (j = 8; j; j--) {
if (crc & 0x80)
crc = (crc << 1) ^ POLYNOMIAL;
else
crc <<= 1;
}
crc8_table[i] = crc;
}
}
// 计算CRC8校验值
uint8_t crc8(const void *data, size_t len) {
const uint8_t *byte = data;
uint8_laipuhuo.com t crc = 0x00;
for (; len > 0; len--) {
crc = crc8_table[(crc ^ *byte ) & 0xFF];
}
return crc;
}
int main(int argc, char *argv[]) {
int fd;
uint8_t buffer;
size_t bytes_read;
uint8_t crc;
if (argc != 2) {
laipuhuo.com fprintf(stderr, "Usage: %s filenamen", argv);
exit(1);
}
fd = open(argv, O_RDONLY);
bytes_read = read(fd, buffer, sizeof(buffer));
crc = crc8(buffer, bytes_read);
printf("laipuhuo.comCRC: 0xXn", crc);<q refer="1"></q><span class="_q_s_"></span>
close(fd);
return 0;
}