views:

230

answers:

2

Hello,

I am using pySerial to read in data from an attached device. I want to calculate the checksum of each received packet. The packet is read in as a char array, with the actual checksum being the very last byte, at the end of the packet. To calculate the checksum, I would normally sum over the packet payload, and then compare it to the actual checksum.

Normally in a language like C, we would expect overflow, because the checksum itself is only one byte. I'm not sure about the internals of python, but from my experience with the language it looks like it will default to a larger size variable (maybe some internal bigInt class or something). Is there anyway to mimic the expected behavior of adding two chars, without writing my own implementation? Thanks.

+4  A: 

Sure, just take the modulus of your result to fit it back in the size you want. You can do the modulus at the end or at every step. For example:

>>> payload = [100, 101, 102, 103, 104] # arbitrary sequence of bytes
>>> sum(payload) % 256 # modulo 256 to make the answer fit in a single byte
254 # this would be your checksum
Kiv
benhoyt
@benhoyt looks like we were thinking the same thing ;-)
nategood
+2  A: 

to improve upon the earlier example, just bitwise-and it with 0xFF. not sure if python does the optimization by default or not.

sum(bytes) & 0xFF
nategood
I'm not sure what Python does under the hood, but a quick test indicates that both methods take almost the same amount of time. The summation is going to be the bulk of the work in any case.
Kiv
Matt J
Looking at the output of "dis.dis(lambda x:x%256)", it looks like this optimisation isn't applied. The small difference in runtime is probably due to the fact that the actual and/mod operation is a very tiny part of the total cost.
Brian
are you people seriously worried about the performance of a math operation, when your data is coming through a serial port ???? ;)
JimB