CCITT CRC 16位起始值0xffff [英] CCITT CRC 16 Bit Start Value 0xffff

查看:1038
本文介绍了CCITT CRC 16位起始值0xffff的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要计算作为参数传递的数据的CCITT 16位校验和值以及长度。如果我用测试数据123456789填充我的数组TempStr,使用多项式0x8408的长度排除空终止字符,我得到结果字符串6E90(十六进制)。加上空终止字符我得到907A。当我换出多项式到0x1201然后我得到结果29E2(十六进制)和EFE8(十六进制)有和没有终止字符。

I need to calculate a CCITT 16 bit checksum value for data passed as a parameter together with the length. If I fill my array TempStr with the test data "123456789", use the polynomial 0x8408 with the length excluding the null termination character, I get the result string 6E90(Hex). Together with the null termination char I get 907A. When I swap out the polynomial to 0x1201 then I get results 29E2(Hex) and EFE8(Hex) with and without termination character.

我的问题是:
我需要计算CRC带有或没有空终止字符,以获得正确的值?
我在算法中使用多项式0x1201或反多项式0x8408?
是给定数据0x29B1的正确CRC吗?我需要正确的值来确定函数是否正确工作。
算法计算此特定的CRC类型是否正确?
wData =(unsigned int)0xff& * pData ++ ??
如果有人可以向我解释什么是错误,以及如何解决我的问题,我会非常感激。
谢谢

My questions are: Do I need to calculate the CRC with or without the null termination character to obtain the correct value? Do I use the polynomial 0x1201 or the reverse polynomial 0x8408 in the algorithm? Is the correct CRC of the given data 0x29B1? I need the correct value to determine if the function works correctly.. Is the algorithm to calculate this specific CRC type correct? wData=(unsigned int)0xff & *pData++?? If someone can explain to me what is wrong and how to fix my problem I would much appreciate it. Thank you

这是使用并显示calculate_CRC16函数的代码:

This is the code that uses and displays the calculate_CRC16 function:

CHAR_t TestStr[] = {"123456789"};
unsigned short CrcTest = calculate_CRC16(TestStr,sizeof(TestStr)-1);
QString CrcDisplay = QString("CrcTest : %1").arg(CrcTest);
ui->txtDebug->setText(CrcDisplay);

这是calculate_CRC16函数:

This is the calculate_CRC16 function:

UINT16_t MainWindow::calculate_CRC16(CHAR_t* pData, UINT16_t wLength)
{

  UCHAR_t i;
  UINT16_t wData;
  UINT16_t wCrc = 0xffff;

  if (wLength == 0)
    return (~wCrc);

  do
  {
    for (i=0, wData=(unsigned int)0xff & *pData++; i < 8; i++, wData >>= 1)
    {
        if ((wCrc & 0x0001) ^ (wData & 0x0001))
            wCrc = (wCrc >> 1) ^ CRC_POLY;
        else  wCrc >>= 1;
    }
  } while (--wLength);

  wCrc = ~wCrc;
  wData = wCrc;
  wCrc = (wCrc << 8) | (wData >> 8 & 0xff);

  return (wCrc);
}


推荐答案

0x29b1 用于falseCCITT CRC-16 (链接到CRC目录)。这显然是你需要的。从目录:

The result of 0x29b1 is for the "false" CCITT CRC-16 (link to CRC catalog). Which is apparently the one you need. From the catalog:

width=16 poly=0x1021 init=0xffff refin=false refout=false xorout=0x0000 check=0x29b1 name="CRC-16/CCITT-FALSE"

所以没有位反转c> refin , refout false)。 CRC用 0xffff 初始化,不进行后处理。

So there is no bit reversal (refin, refout false). The CRC is initialized with 0xffff and is not post-processed.

要修改代码,

if (wLength == 0)
    return wCrc;

do
{
    for (i=0, wData=((unsigned int)0xff & *pData++) << 8; i < 8; i++, wData <<= 1)
    {

        if ((wCrc & 0x8000) ^ (wData & 0x8000))
            wCrc = (wCrc << 1) ^ 0x1021;
        else  wCrc <<= 1;
    }
} while (--wLength);

return wCrc & 0xffff;

或更合理:

while (wLength--) {
    wCrc ^= *(unsigned char *)pData++ << 8;
    for (i=0; i < 8; i++)
        wCrc = wCrc & 0x8000 ? (wCrc << 1) ^ 0x1021 : wCrc << 1;
}
return wCrc & 0xffff;

这篇关于CCITT CRC 16位起始值0xffff的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆