This post originated from an RSS feed registered with Python Buzz
by Phillip Pearson.
Original Post: Generating Motorola S19 checksums in Python
Feed Title: Second p0st
Feed URL: http://www.myelin.co.nz/post/rss.xml
Feed Description: Tech notes and web hackery from the guy that brought you bzero, Python Community Server, the Blogging Ecosystem and the Internet Topic Exchange
A friend just popped up on MSN and asked me how to generate S19 checksums in Python. It's a pretty trivial bit of code, but I know I'll want it again, or maybe someone is out there googling for a way to do it, so here we go:
def s19_checksum(line):
length = eval("0x" + line[2:4])
bytes = [eval('0x' + line[i*2:i*2+2]) for i in range(1, length+1)]
return ~reduce((lambda a,b: a+b), bytes) & 0xff
for s in ('S1130170707172737475767778797A7B7C7D7E7F03',
'S111FE7B59597B0173F6014A7B0146B7573093',
'S106FE891B823D98',
'S109F8020107DF01BA0D6C',):
print "* checksumming %s" % s
print "checksum: %s (compare with %s)" % (hex(s19_checksum(s)), s[-2:])