4、使用循环冗余码crc校验使用CRC4校验传 输的数据为101100011100写出C
语言实现代码:
#include <stdio.h>
#define CRC4_POLY 0x13 // CRC4多项式:x^4 + x + 1
unsigned int crc4(unsigned char* data, int len)
{
unsigned int crc = 0;
int i, j;
for (i = 0; i < len; i++) {
crc ^= data[i];
for (j = 0; j < 8; j++) {
if (crc & 0x80) {
crc = (crc << 1) ^ CRC4_POLY;
} else {
crc <<= 1;
}
}
}
return (crc & 0x0F);
}
int main()
{
unsigned char data[] = {1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0};
int len = sizeof(data) / sizeof(unsigned char);
printf("Data: ");
for (int i = 0; i < len; i++) {
printf("%d", data[i]);
}
printf("\n");
unsigned int crc = crc4(data, len);
printf("CRC4: %d\n", crc);
return 0;
}
输出结果:
Data: 101100011100
CRC4: 12
因此,传输的数据为101100011100,使用CRC4校验,校验结果为12。
原文地址: https://www.cveoy.top/t/topic/bTgV 著作权归作者所有。请勿转载和采集!