crc16.py
来自「This is a software implementation of the」· Python 代码 · 共 47 行
PY
47 行
#!/usr/bin/python# ======================================================================# crc16.py - Prototype implementations of CRC16 calculation## Copyright 2006-2008 Dick Streefland## This is free software, licensed under the terms of the GNU General# Public License as published by the Free Software Foundation.# ======================================================================import syspolynomial = 0xA001 # X^16 + X^15 + X^2 + 1 - lower 16 bits, reverseddef crc_update(crc, value, bits): for bit in range(bits): xor = (crc ^ (value >> bit)) & 1 crc >>= 1 if xor: crc ^= polynomial return crcdef crc1(crc, byte): return crc_update(crc, byte, 8)def crc4(crc, byte): crc = (crc >> 4) ^ tab4[(crc ^ byte) & 0xf] crc = (crc >> 4) ^ tab4[(crc ^ (byte >> 4)) & 0xf] return crcdef crc8(crc, byte): return (crc >> 8) ^ tab8[(crc ^ byte) & 0xff]def crc_file(file, crcfunc): crc = 0xffff for byte in open(file).read(): crc = crcfunc(crc, ord(byte)) crc ^= 0xffff print "%02x %02x %s" % (crc & 0xff, crc >> 8, file)tab4 = [crc_update(i, 0, 4) for i in range(2**4)]tab8 = [crc_update(i, 0, 8) for i in range(2**8)]for file in sys.argv[1:]: crc_file(file, crc1) crc_file(file, crc4) crc_file(file, crc8)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?