本文介绍了根据J.G.校验算法弗莱彻的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直负责在实施是基于一个J.G.校验算法弗莱彻校验和ISO 8473-1:1998年,像这样描述:

然后他们列表看到,可以检查4个数据,如果该算法中是正确的,但我的版本在最后两个值失败。结果
0000给人FFFF结果的校验
0000'00给人FFFF结果的校验
ABCDEF'01给人9CF8结果的校验
1456'F89A'0001给人24DC的校验

我一直工作在这几个小时,现在并不能找到我做错了什么,一套新的眼睛可以帮助极大。

下面是我的功能:

  UINT16 Crc_CalculateISOChecksum(UINT8 * pt_start_address,UINT32长度)
{
    UINT8 C0,C1;
    UINT8数据;
    UINT32我;
    UINT8 CK1,CK2;    /* 初始值 */
    C0 = 0;
    C1 = 0;    / *回忆 - 全32位* /
    对于(i = 0; I<长度;我+ +)/ * nb_bytes得到了验证* /
    {
      数据= pt_start_address [I]
      C0 =(C0 +数据)255%;
      C1 =(C1 + C0)%255;
    }
    / *计算中间ISO校验和值* /
    CK1 =(unsigned char型)(255 - ((C0 + C1)255%));
    CK2 =(unsigned char型)(C1%255);
    如果(CK1 == 0)
    {
      CK1 = MASK_BYTE_LSB;
    }
    如果(CK2 == 0)
    {
        CK2 = MASK_BYTE_LSB;
    }
    返回((((UINT16)CK1)LT;< 8)|((UINT16)CK2));
}


解决方案

您中间和值应 uint16_t (或行话UINT16)。

  uint16_t C0,C1; //不uint8_t有。

根据什么字符 INT 您的系统上(如不设想int比更多的位字符)的中间和可能的四溢。您的实现依赖于uint8_t有被提升。

要说明:

 为0xFF 0xFF的
+ +为0xFF 0xFF的
===== =====
0x1FE%255 = 0 0xFE的255%= 254
  ^ ^保留降

I have been tasked in implementing a Checksum algorithm that is based on the J.G. Fletcher checksum and ISO 8473-1:1998 and is described like so :

They then list 4 data that can be checked to see if the algo is correct but my version fails at the last two values.
0000 gives a checksum of FFFF
0000'00 gives a checksum of FFFF
ABCDEF'01 gives a checksum of 9CF8
1456'F89A'0001 gives a checksum of 24DC

I've been working on this for hours now and can't find what I did wrong, a new set of eyes could help tremendously.

Here is my function:

uint16 Crc_CalculateISOChecksum(uint8 *pt_start_address, uint32  length)
{
    uint8 C0, C1;
    uint8 data;
    uint32 i;
    uint8 ck1, ck2;

    /* Initial value */
    C0 = 0;
    C1 = 0;

    /* memories - 32bits wide*/
    for (i=0; i<length; i++)    /* nb_bytes has been verified */
    {
      data = pt_start_address[i];   
      C0 = (C0 + data)%255;
      C1 = (C1 + C0)%255;
    }
    /* Calculate the intermediate ISO checksum value */
    ck1 = (unsigned char)(255-((C0+C1)%255));
    ck2 =  (unsigned char)(C1%255);
    if (ck1 == 0)
    {
      ck1 = MASK_BYTE_LSB;
    }
    if (ck2 == 0)
    {
        ck2 = MASK_BYTE_LSB;
    }
    return ((((uint16)ck1)<<8) | ((uint16)ck2));    
}
解决方案

Your intermediate sums should be uint16_t (or uint16 in your lingo).

uint16_t C0, C1; // Not uint8_t.

Depending on what char and int on your system are (e.g. to not assume that int has more bits than char) your intermediate sums may be overflowing. Your implementation relies on uint8_t being promoted.

To illustrate:

 0xFF               0xFF
+0xFF              +0xFF
=====              =====
0x1FE % 255 = 0     0xFE % 255 = 254
  ^Retain            ^Drop

这篇关于根据J.G.校验算法弗莱彻的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 06:35