Skip to main content

python版crc16/crc32校验的实现

Project description

本模块主要进行数据校验:支持查表法和直接法。支持crc16常用校验法和自定义校验。详细用法可参照代码。

crc32仅仅实现了经典法。

import unittest
from zp_pycrc import *
class crc_test(unittest.TestCase):
    def test_crc16(self):
        datas=b'\x01\x02\x03\x04\x05'
        print(len(datas),datas)
        crc=crc16_ccitt()
        print(crc.__class__.__name__,"%04X"%(crc.cal(datas)))
        crc=crc16_ccitt_false()
        print(crc.__class__.__name__,"%04X"%(crc.cal(datas)))
        crc=crc16_xmodem()
        print(crc.__class__.__name__,"%04X"%(crc.cal(datas)))
        crc=crc16_x25()
        print(crc.__class__.__name__,"%04X"%(crc.cal(datas)))
        crc=crc16_ibm()
        print(crc.__class__.__name__,"%04X"%(crc.cal(datas)))
        crc=crc16_usb()
        print(crc.__class__.__name__,"%04X"%(crc.cal(datas)))
        crc=crc16_maxim()
        print(crc.__class__.__name__,"%04X"%(crc.cal(datas)))
        crc=crc16_modbus()
        print(crc.__class__.__name__,"%04X"%(crc.cal(datas)))
        crc=crc16_dnp()
        print(crc.__class__.__name__,"%04X"%(crc.cal(datas)))
            
        crc=crc16( 0,0x8005, True, True, 0)
        print(crc.__class__.__name__,"%04X"%(crc.cal(datas)))
        crc=crc16_ibm()
        print("crc16_ibm __call__",hex(crc(datas)))
        b=cal_crc16_classcial(datas, 0,0x8005, True, True, 0)
        print("cal_crc16_classcial",hex(b))
        print('*******crc  ibm table*****')
        print([hex(i) for i in crc.crc_table])
        
    def test_crc32(self):
        datas=b'\x01\x02\x03\x04\x05'
        r=cal_crc32_classcial(datas, 0xffffffff, 0x04c11db7, True, True, 0xffffffff)
        print(hex(r))

if __name__ == '__main__':
    unittest.main()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zp_pycrc-0.0.4.tar.gz (3.6 kB view details)

Uploaded Source

File details

Details for the file zp_pycrc-0.0.4.tar.gz.

File metadata

  • Download URL: zp_pycrc-0.0.4.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.8

File hashes

Hashes for zp_pycrc-0.0.4.tar.gz
Algorithm Hash digest
SHA256 a0fe10419abe9c18faeafd72c58ae9e894e6ba98a77571aa5bbf22ec65c97d54
MD5 af9545cf527be5d965f9c03197e9fa93
BLAKE2b-256 cda7b47d5f140c715866dedba3523ffb3bb0049a9452e8d132852c48ea361fb2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page