Pyhon 计算CRC16
def crc16_direct(bytestr):
'''
crc16直接计算法
:param bytestr: bytes字符串
:return: int16类型
'''
crc = 0
if len(bytestr) == 0:
return 0
for i in range(len(bytestr)):
R = bytestr[i]
for j in range(8):
if R > 127:
k = 1
else:
k = 0
R = (R << 1) & 0xff
if crc > 0x7fff:
m = 1
else:
m = 0
if k + m == 1:
k = 1
else:
k = 0
crc = (crc << 1) & 0xffff
if k == 1:
crc ^= 0x1021 # 多项式为 0x1021
return crc