CRC-CCITT 16位Python手动计算

2024-04-24 07:19:23 发布

您现在位置:Python中文网/ 问答频道 /正文

问题

我正在为一个嵌入式设备编写代码。许多用于CRC-CCITT 16位计算的解决方案都需要库。

考虑到使用库几乎是不可能的,而且会消耗库的资源,因此需要一个函数。

可能的解决方案

以下CRC计算在网上找到。但是,它的实现是不正确的。

http://bytes.com/topic/python/insights/887357-python-check-crc-frame-crc-16-ccitt

def checkCRC(message):
    #CRC-16-CITT poly, the CRC sheme used by ymodem protocol
    poly = 0x11021
    #16bit operation register, initialized to zeros
    reg = 0xFFFF
    #pad the end of the message with the size of the poly
    message += '\x00\x00' 
    #for each bit in the message
    for byte in message:
        mask = 0x80
        while(mask > 0):
            #left shift by one
            reg<<=1
            #input the next bit from the message into the right hand side of the op reg
            if ord(byte) & mask:   
                reg += 1
            mask>>=1
            #if a one popped out the left of the reg, xor reg w/poly
            if reg > 0xffff:            
                #eliminate any one that popped out the left
                reg &= 0xffff           
                #xor with the poly, this is the remainder
                reg ^= poly
    return reg

现有的在线解决方案

以下链接正确计算16位CRC。

http://www.lammertbies.nl/comm/info/crc-calculation.html#intr

“CRC-CCITT(XModem)”下的结果是正确的CRC。

规格

我相信现有在线解决方案中的“CRC-CCITT(XModem)”计算使用了0x1021多项式。

问题

如果有人可以编写一个新函数,或者提供方向来按照所需的规范解决checkCRC函数。请注意,使用库或任何import都没有帮助。


Tags: ofthe函数httpmessageifmask解决方案
3条回答

原始函数checkCRC也可以执行“CRC-CCITT(XModem)”。

刚刚设置:

poly = 0x1021
reg = 0

而不是

poly = 0x11021
reg = 0xFFFF

这是C库的一个python端口,来自CRC-CCITT XMODEM的http://www.lammertbies.nl/comm/info/crc-calculation.html

这个库对于实际的用例很有趣,因为它预先计算crc表以提高速度。

用法(带字符串或字节列表):

crc('123456789')
crcb(0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39)

测试给出:'0x31c3'

POLYNOMIAL = 0x1021
PRESET = 0

def _initial(c):
    crc = 0
    c = c << 8
    for j in range(8):
        if (crc ^ c) & 0x8000:
            crc = (crc << 1) ^ POLYNOMIAL
        else:
            crc = crc << 1
        c = c << 1
    return crc

_tab = [ _initial(i) for i in range(256) ]

def _update_crc(crc, c):
    cc = 0xff & c

    tmp = (crc >> 8) ^ cc
    crc = (crc << 8) ^ _tab[tmp & 0xff]
    crc = crc & 0xffff
    print (crc)

    return crc

def crc(str):
    crc = PRESET
    for c in str:
        crc = _update_crc(crc, ord(c))
    return crc

def crcb(*i):
    crc = PRESET
    for c in i:
        crc = _update_crc(crc, c)
    return crc

如果在开始时用poly = 0x1021替换poly = 0x11021,则建议的checkCRC例程是CRC-CCITT变体“1D0F”。

下面是一个可以转换为Python的C版本:

#define POLY 0x1021

/* CRC-16 XMODEM: polynomial 0x1021, init = 0, xorout = 0, no reflection */
unsigned crc16x(unsigned crc, unsigned char *buf, size_t len)
{
    while (len--) {
        crc ^= *buf++ << 8;
        crc = crc & 0x8000 ? (crc << 1) ^ POLY : crc << 1;
        crc = crc & 0x8000 ? (crc << 1) ^ POLY : crc << 1;
        crc = crc & 0x8000 ? (crc << 1) ^ POLY : crc << 1;
        crc = crc & 0x8000 ? (crc << 1) ^ POLY : crc << 1;
        crc = crc & 0x8000 ? (crc << 1) ^ POLY : crc << 1;
        crc = crc & 0x8000 ? (crc << 1) ^ POLY : crc << 1;
        crc = crc & 0x8000 ? (crc << 1) ^ POLY : crc << 1;
        crc = crc & 0x8000 ? (crc << 1) ^ POLY : crc << 1;
    }
    return crc & 0xffff;
}

crc初始化为零。

相关问题 更多 >