CRC 16程序计算校验和

标签 c crc crc16

我目前正在编写一个 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/

相关文章:

c - 为什么将随机值显示为输出来查找矩阵的行列式?

c - 如何在我的项目中用C调用别人开源专有项目的函数

c - 填充字符串留在 C 中的麻烦

c# - C# 中的 CRC-4 实现

c - 未定义对 crcsum 的引用(unsigned char const*,unsigned long,unsigned short)

php - 如何检查CRC16有效性

c++ - 如何将指针视为多数组?

python - 如何在python中重建libmem_crc32_direct CRC函数?

c++ - 为什么编译器以不同的方式优化这些情况?

byte - crc16实现java