Skip to content

Instantly share code, notes, and snippets.

@nzjrs nzjrs/test_crc.py
Created Oct 28, 2014

Embed
What would you like to do?
Non-table based implementations of crc16 and crc8-maxim
import crcmod.predefined
def _crc16(crc, c):
crc ^= ord(c)
for i in range(8):
if crc & 0x1:
crc = (crc >> 1) ^ 0xA001
else:
crc = (crc >> 1)
return crc
def crc16(s):
crc = 0
for c in s:
crc = _crc16(crc,c)
return crc
def _crc8maxim(crc, c):
crc ^= ord(c)
for i in range(8):
if crc & 0x1:
crc = (crc >> 1) ^ 0x8C
else:
crc = (crc >> 1)
return crc
def crc8maxim(s):
crc = 0
for c in s:
crc = _crc8maxim(crc,c)
return crc
if __name__ == "__main__":
S = 'test124'
print "CRC16 (%s)" % S
crc = crcmod.predefined.Crc('crc-16')
crc.update(S)
print hex(crc.crcValue), 'vs', hex(crc16(S))
print "CRC8 (%s)" % S
crc = crcmod.predefined.Crc('crc-8-maxim')
crc.update(S)
print hex(crc.crcValue), 'vs', hex(crc8maxim(S))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.