HJ 212-2017 CRC数据校验 (Python)

借鉴博客地址:https://www.cnblogs.com/LiuSiyuan/p/8110192.html,翻译成Python版

代码如下:

def crc16(text):
    """
    hj 212-2017 crc16效验
    :param text: 待效验的字符串
    :return: result
    """
    data = bytearray(text, encoding='utf-8')
    crc = 0xffff
    dxs = 0xa001
    for i in range(len(data)):
        hibyte = crc >> 8
        crc = hibyte ^ data[i]
        for j in range(8):
            sbit = crc & 0x0001
            crc = crc >> 1
            if sbit == 1:
                crc ^= dxs
    return hex(crc)[2:]


if __name__ == '__main__':
    result = crc16('QN=20191015160812988;ST=22;CN=2011;PW=111111;MN=18092222;Flag=5;CP=&&DataTime=20191015160812;a34004-Rtd=15.5;a34002-Rtd=21.7;LA-Rtd=55.4;a01001-Rtd=0.0;a01002-Rtd=0.0;a01007-Rtd=0.0;a01008-Rtd=0;a34001-Rtd=0.0&&')
    print(result)

 

你可能感兴趣的:(Python,hj212)