# two's complement bytes - Python

This is a discussion on two's complement bytes - Python ; 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 ...

1. ## two's complement bytes

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?

2. ## Re: two's complement bytes

On Aug 23, 10:51 pm, "Adam W." <AWasile...@gmail.com> wrote:
> 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?

Try this out. Does it come close to what you want?

import struct
struct.pack( 'i', ~10 )
~struct.unpack( 'i', _ )[ 0 ]

>>> import struct
>>> struct.pack( 'i', ~10 )

'\xf5\xff\xff\xff'
>>> ~struct.unpack( 'i', _ )[ 0 ]

10
>>>

3. ## Re: two's complement bytes

On Aug 24, 12:23 am, castironpi <castiro...@gmail.com> wrote:
> Try this out.  Does it come close to what you want?
>
> import struct
> struct.pack( 'i', ~10 )
> ~struct.unpack( 'i', _ )[ 0 ]
>
>
>
>
>
> >>> import struct
> >>> struct.pack( 'i', ~10 )

> '\xf5\xff\xff\xff'
> >>> ~struct.unpack( 'i', _ )[ 0 ]

> 10- Hide quoted text -
>
> - Show quoted text -

Humm, so how do you use it :P Let me give you some examples and then
you can run it through:

0b1111110010010000 or 0xFC90 Should equal -880
0b0000011111010000 or 0x07D0 Should equal +2000

4. ## Re: two's complement bytes

On Aug 23, 11:52 pm, "Adam W." <AWasile...@gmail.com> wrote:
> On Aug 24, 12:23 am, castironpi <castiro...@gmail.com> wrote:
>
> > Try this out.  Does it come close to what you want?

>
> > import struct
> > struct.pack( 'i', ~10 )
> > ~struct.unpack( 'i', _ )[ 0 ]

>
> > >>> import struct
> > >>> struct.pack( 'i', ~10 )

> > '\xf5\xff\xff\xff'
> > >>> ~struct.unpack( 'i', _ )[ 0 ]

> > 10- Hide quoted text -

>
> > - Show quoted text -

>
> Humm, so how do you use it :P  Let me give you some examples and then
> you can run it through:
>
> 0b1111110010010000 or 0xFC90  Should equal -880
> 0b0000011111010000 or 0x07D0  Should equal +2000

In this case I look at:

>>> struct.unpack( '>h', '\xfc\x90' )[0]

-880
>>> struct.unpack( '>h', '\x07\xd0' )[0]

2000

5. ## Re: two's complement bytes

On Aug 24, 1:11 am, castironpi <castiro...@gmail.com> wrote:
> On Aug 23, 11:52 pm, "Adam W." <AWasile...@gmail.com> wrote:
>
>
>
>
>
> > On Aug 24, 12:23 am, castironpi <castiro...@gmail.com> wrote:

>
> > > Try this out.  Does it come close to what you want?

>
> > > import struct
> > > struct.pack( 'i', ~10 )
> > > ~struct.unpack( 'i', _ )[ 0 ]

>
> > > >>> import struct
> > > >>> struct.pack( 'i', ~10 )
> > > '\xf5\xff\xff\xff'
> > > >>> ~struct.unpack( 'i', _ )[ 0 ]
> > > 10- Hide quoted text -

>
> > > - Show quoted text -

>
> > Humm, so how do you use it :P  Let me give you some examples and then
> > you can run it through:

>
> > 0b1111110010010000 or 0xFC90  Should equal -880
> > 0b0000011111010000 or 0x07D0  Should equal +2000

>
> In this case I look at:
>
> >>> struct.unpack( '>h', '\xfc\x90' )[0]

> -880
> >>> struct.unpack( '>h', '\x07\xd0' )[0]

>
> 2000- Hide quoted text -
>
> - Show quoted text -

Perfect, thank you! I will have to read up on struct to see how you
did that.