two's complement bytes

Adam W. AWasilenko at gmail.com
Sat Aug 23 23:51:23 EDT 2008


I'm dabbling with AVR's for a project I have and that means I have to
use C (ageist my will).  Because my AVR will be tethered to my laptop,
I am writing most of my logic in python, in the hopes of using at
little C as possible.

In my quest I came across a need to pass a pair of sign extended two's
complement bytes.  After painfully reading the wikipedia article on
what two's complement was, I then thought of how I would handle this
in python.  I don't really recall ever having to work in binary with
python, so I really am clueless on what to do.

I can feed python either two hex bytes or binary, but how do I convert
it into an int, and more importantly how do I make sure it handles the
sign properly?



More information about the Python-list mailing list