jablotron80 icon indicating copy to clipboard operation
jablotron80 copied to clipboard

Simplify checksum code

Open perexg opened this issue 1 year ago • 5 comments

Hi, the checksum code used in the original Windows application is pretty simple:

        def checksum(msg):
            sum = 0x7f
            for byte in msg:
                for j in range(8):
                    sum <<= 1
                    if (byte & 0x80) == 0x80:
                        sum += 1
                    if (sum & 0x80) == 0x80:
                        sum ^= 0xa3
                    byte <<= 1
            return sum

        # binary message without trailing 0xFF
        if len(msg) > 1 and checksum(msg) != 0:
            raise SomeCheckSumError

perexg avatar Apr 03 '23 19:04 perexg