【问题标题】:Helping for 7bits CRC in 40Bits message(message need to be initial with seed value)帮助 40Bits 消息中的 7bits CRC(消息需要以种子值开头)
【发布时间】:2021-07-22 14:22:27
【问题描述】:

您好,我正在为我的项目实施 CRC 计算功能。 参数如下

Polynomial : x7 + x5 + x4 + x2 + x +1  --> 0xB7(0b10110111)
Seed Value : 0b1110000                 --> 0x70
Message Example                        --> 0x 84 80 00 00 00(message[5]={0x00(pending zero for CRC bit),0x00,0x00,0x08,0x84})

到目前为止,我知道我需要用 " message[4] (0x84) XOR seedvalue " 的种子值初始化消息, 然后消息变为message[4] = {0x00,0x00,0x00,0x08,0x64},我搜索了一些关于CRC算法和C语言示例代码的信息。 所以我用上面的参数写了CRC计算函数,这是我的代码:

#include <stdio.h>
typedef unsigned char uint8_t;
uint8_t crc=0 ;
uint8_t POLYNOMIAL =0xB7 ;

uint8_t crcCount(uint8_t message[],uint8_t byte)
{
    for(int i =0; i <byte ; i++)
    {
        crc ^=message[i] ;
        for( int bit =0 ; bit< 8; bit++)
        {
            if((crc & 0x40U))
            {
                crc = (crc << 1) ^ POLYNOMIAL ;
            }
            else
            {
                crc <<=1u ;
            }
        }

    }
    return crc ;
}

int main()
{
    uint8_t c_value ;
    uint8_t message[5]={0x00,0x00,0x00,0x08,0x64};
    c_value = crcCount(message,5);
    printf("CRC = 0x%x",c_value);
    return 0;
}

我得到了错误的答案CRC = 0x33,因为答案是0x7D。 有人可以教我哪一部分是错的吗?多谢!! 我在这个问题上停留了一段时间。

【问题讨论】:

  • 我无法理解你的Message Example - 你如何从84 80 00 00 000x00 ,0x00,0x00,0x08,0x84message[4] = {0x00,0x00,0x00,0x08,0x64} 应该是什么意思?

标签: c algorithm crc


【解决方案1】:

对不起!!我发布了错误的代码,上面的代码是我的第一个版本!!! 消息应该是 0x8408000000, message[5]={0x84,0x08,0x00,0x00,0x00}, 最初是message[5]={0x64,0x08,0x00,0x00,0x00}; 0x84 ^ (0x70 &lt;&lt;1) = 0x64之后的最后一个版本代码如下:

#include <stdio.h>

typedef unsigned char uint8_t;
uint8_t crc=0 ;
uint8_t POLYNOMIAL =0xB7 ;

uint8_t crcCount(uint8_t message[],uint8_t byte)
{
    for(int i=0; i < byte ; i ++)
    {
        crc ^=message[i] ;
        for( int bit =0 ; bit< 8; bit++)
        {
            if((crc & 0x80))
            {
                crc = (crc <<1) ^ (POLYNOMIAL<<1) ;
            }
            else
            {
                crc <<=1u ;
            }
        }
  
    }
    return crc >>1  ;
}


int main()
{
    uint8_t c_value ;
    uint8_t message[5]={0x64,0x08,0x00,0x00,0x00};
    c_value = crcCount(message,5);
    printf("CRC = 0x%x",c_value);
    return 0;
}

答案是0x7D,但应该是0x56,还是错了

对不起,愚蠢的错误............

【讨论】:

  • 不要添加答案。只需编辑问题即可更正。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-16
  • 2010-10-17
  • 1970-01-01
相关资源
最近更新 更多