首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >CRC-16/CCITT-FALSE与CRC-16/X-25有何区别?

CRC-16/CCITT-FALSE与CRC-16/X-25有何区别?
EN

Stack Overflow用户
提问于 2019-11-08 22:01:13
回答 1查看 4.1K关注 0票数 0

我想实现CRC-16/X-25。我有一个用于CRC-16/CCITT_FALSE的C代码,但是我不知道为了实现CRC-16/X-25,我需要对该代码进行哪些更改?

下面给出了我用于CRC-16/CCITT-FALSE的代码:

代码语言:javascript
运行
复制
#include  <msp430.h>

#define CRC_POLYNOMIAL      0x1021
#define USHORT_TOPBIT       0x8000

short crc_ret;

// function initialisation

unsigned short crc16(const char *buf, unsigned int buf_len)
{

    unsigned short ret = 0xFFFF;        // Initial Value
    int bit = 8;

    do {
        if (!buf) {
            break;
        }
        unsigned int i = 0;
        for (i=0; i<buf_len; ++i) {
            ret = (unsigned short)(ret ^ ((unsigned short)buf[i] >> 8));
            for (bit = 8; bit > 0; --bit)
            {
                if (ret & USHORT_TOPBIT)
                    ret = (unsigned short)((ret >> 1) ^ CRC_POLYNOMIAL);
                else
                    ret = (unsigned short)(ret >> 1);
            }
        }
    } while (0);
    return ret;
 }

int main(void)
{
  PM5CTL0 &= ~LOCKLPM5;               // Disable the GPIO power-on default high-impedance mode to activate
                                      // previously configured port settings

  char buf[16];


  buf[0] = 0x5A;                    //buf[0], buf[1] and buf[2] are data bytes

  buf[1] = 0xCF;
  buf[2] = 0x00;



  crc_ret = crc16(buf, 3);   // Calling CRC function

}
EN

Stack Overflow用户

回答已采纳

发布于 2019-11-09 00:34:16

根据下面的网站,CRC16/CCITT_FALSE与您的代码不匹配。网站指出它是一个左移(非反射) CRC,而你的代码是右移。

CRC16/X25是右移(反射) CRC,返回~CRC (CRC xor 0xFFFF)。我不知道这是否与CRC16/CCITT_X25相同。

http://www.sunshine2k.de/coding/javascript/crc/crc_js.html

您可以使用该网站将您的结果与其结果进行比较。

我从一个简单的示例开始创建两个crc16函数:

代码语言:javascript
运行
复制
uint16_t crc16_ccitt_false(char* pData, int length)
{
    int i;
    uint16_t wCrc = 0xffff;
    while (length--) {
        wCrc ^= *(unsigned char *)pData++ << 8;
        for (i=0; i < 8; i++)
            wCrc = wCrc & 0x8000 ? (wCrc << 1) ^ 0x1021 : wCrc << 1;
    }
    return wCrc;
}

uint16_t crc16_x25(char* pData, int length)
{
    int i;
    uint16_t wCrc = 0xffff;
    while (length--) {
        wCrc ^= *(unsigned char *)pData++ << 0;
        for (i=0; i < 8; i++)
            wCrc = wCrc & 0x0001 ? (wCrc >> 1) ^ 0x8408 : wCrc >> 1;
    }
    return wCrc ^ 0xffff;
}
票数 2
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58768018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档