我目前正在编写一个 CRC16 程序,该程序使用 CRC 16 多项式 X^16 + X^15 + X^2 + 1
计算字符的 CRC。程序应从标准输入读取数据并以十六进制输出 16 位 CRC。尽管如此,当我执行该程序时,我得到了错误的输出值。
这是我的代码:
#include <stdint.h>
#define CRC16 0x8005
unsigned short crc(unsigned char msg[], int len)
{
unsigned short out = 0;
int bits = 0, t_flag;
int x = 0;
/* Sanity check: */
if(msg == NULL)
return 0;
while(len > x)
{
unsigned short data = msg[x];
t_flag = out >> 15;
/* Get next bit: */
out <<= 1;
out |= (data >> bits) & 1; // item a) work from the least significant bits
/* Increment bit counter: */
bits++;
if(bits > 7)
{
bits = 0;
data++;
len--;
}
/* Cycle check: */
if(t_flag)
out ^= CRC16;
}
// item b) "push out" the last 16 bits
int i;
for (i = 0; i < 16; ++i) {
t_flag = out >> 15;
out <<= 1;
if(t_flag)
out ^= CRC16;
}
// item c) reverse the bits
unsigned short crc1 = 0;
i = 0x8000;
int j = 0x0001;
for (; i != 0; i >>=1, j <<= 1) {
if (i & out) crc1 |= j;
}
return crc1;
}
int main (int argc, char *argv[]) {
//if (argv[1] == "trace") {
//printf(argv[1]);
//}
char ARGV;
if(argc < 1) {
printf("Must have atleast one arguments\n");
return 1;
}
char buf[256];
int c , r;
int count = -1;
while((c = getchar())!=EOF) {
buf[count++] = putchar(c);
}
r = crc(buf, count);
//printf("%s\n",argv[1] );
printf("%04hx\n", r);
//print("%x\n", argv[1]);
return (0);
//printf(" %4x\n", crc(argv[1], 16));
}
输出:
(我正在阅读我的 txt 文件中的 123456789)
./crc1 < testfile.txt
123456789
7bda
它应该是 BB3D
但我得到的是 7bda
。有人可以帮我弄清楚我做错了什么吗?
最佳答案
#include<stdio.h>
#include<stdint.h>
#define CRC16 0x8005
uint16_t gen_crc16(const uint8_t *data, uint16_t size)
{
uint16_t out = 0;
int bits_read = 0, bit_flag;
// test
printf("buffer in function %s\n", data);
/* Sanity check: */
if(data == NULL)
return 0;
while(size > 0)
{
bit_flag = out >> 15;
/* Get next bit: */
out <<= 1;
out |= (*data >> bits_read) & 1; // item a) work from the least significant bits
/* Increment bit counter: */
bits_read++;
if(bits_read > 7)
{
bits_read = 0;
data++;
size--;
}
/* Cycle check: */
if(bit_flag)
out ^= CRC16;
}
// item b) "push out" the last 16 bits
int i;
for (i = 0; i < 16; ++i) {
bit_flag = out >> 15;
out <<= 1;
if(bit_flag)
out ^= CRC16;
}
// item c) reverse the bits
uint16_t crc = 0;
i = 0x8000;
int j = 0x0001;
for (; i != 0; i >>=1, j <<= 1) {
if (i & out) crc |= j;
}
return crc;
}
int main()
{
char buf[]="123456789";
int c , r;
printf ("the buf has %s", buf);
r = gen_crc16(buf,sizeof(buf)-1);
printf("%04hx\n", r);
return (0);
}
关于CRC 16程序计算校验和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21939392/