two's complement bytes

castironpi castironpi at gmail.com
Sun Aug 24 07:11:49 CEST 2008


On Aug 23, 11:52 pm, "Adam W." <AWasile... at gmail.com> wrote:
> On Aug 24, 12:23 am, castironpi <castiro... at 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



More information about the Python-list mailing list