繁体   English   中英

CRC计算

[英]CRC Calculation

我正在尝试与第三方系统连接,并且他们提供了一个代码示例来计算发送文本数据时的CRC值。

供应商提供的C代码如下所示:

#define CRCRES 0xf0b8  /* residue for good verify */ 
#define DEBUG 

unsigned crctbl[] = {0x0000, 0x1081, 0x2102, 0x3183, 
                     0x4204, 0x5285, 0x6306, 0x7387, 
                     0x8408, 0x9489, 0xa50a, 0xb58b, 
                     0xc60c, 0xd68d, 0xe70e, 0xf78f}; 

/* 
 * This uses a 32 byte table to lookup the crc 4 bits at a time. 
 * The CRC CCITT is used. 
 */ 


unsigned short calc_crc(unsigned char *ptr, unsigned length) 

{ 
  unsigned short crc; 
  unsigned short i; 
  unsigned char pos,ch; 

  crc = 0xffff;  /* precondition crc */ 
  for (i = 0; i < length; i++,ptr++) { 
    ch = *ptr; 
    pos = (crc ^ ch) & 15; 
    crc = ((crc >> 4) & 0x0fff) ^ crctbl[pos]; 
    ch >>= 4; 
    pos = (crc^ch) & 15; 
    crc = ((crc >> 4) & 0xffff) ^ crctbl[pos]; 
  } 
  crc = ~crc;  /* post condition */ 
  crc = (crc << 8) | (crc >> 8); /* bytewise reverse */ 
  return crc; 
} 


/* 
 * tests the block of code containing the crc to verify it's 
 * content.  This compares to the reversed and inverted 
 * residue. 
 */ 

int test_crc(unsigned char *ptr, unsigned length) 

{ 
  unsigned short crc; 
  unsigned char arr [] = {'X','Y','Z'}; 

  crc = calc_crc(arr,3); 
  printf("Calced crc of test to be %04x, (should be 470f)\n", crc); 
  return (crc == 0x470f); 
} 

我已复制此代码并输入示例C程序。 test_crc方法不将CRC计算为470f(将其计算为DD7A)。

我希望有人可以验证此代码不起作用,因为供应商说它应该或帮助我让test_crc返回正确的值。

谢谢您的帮助。

问题解决了。 供应商的规格不正确。 XYZ的正确校验和是DD7A。 文档不正确。

再加上它在页面之前,当他们解释传递给calc_crc方法的数据以获取实际消息的crc时,它也是不正确的,这使得它成为一个棘手的项目。

谢谢您的帮助。

因此,计算不会显示任何错误。 再试一次该算法。 特别是,在寻找可能的问题时,请注意算法的不同部分中的常量是0x0fff0xffff 也许它应该是0x0fff0xfff0更加对称...有时我们认为真正的问题可能来自另一个地方,而忘记仔细检查明显的事情...... :)

硬件平台是否相同? 难道你有不同的字节序吗? (你有x86,例如它们有68000个cpu。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM