By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,341 Members | 1,364 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,341 IT Pros & Developers. It's quick & easy.

two's complement bytes

P: n/a
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?
Aug 24 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Aug 23, 10:51*pm, "Adam W." <AWasile...@gmail.comwrote:
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
>>>
Aug 24 '08 #2

P: n/a
On Aug 24, 12:23*am, castironpi <castiro...@gmail.comwrote:
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
Aug 24 '08 #3

P: n/a
On Aug 23, 11:52*pm, "Adam W." <AWasile...@gmail.comwrote:
On Aug 24, 12:23*am, castironpi <castiro...@gmail.comwrote:
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
Aug 24 '08 #4

P: n/a
On Aug 24, 1:11*am, castironpi <castiro...@gmail.comwrote:
On Aug 23, 11:52*pm, "Adam W." <AWasile...@gmail.comwrote:


On Aug 24, 12:23*am, castironpi <castiro...@gmail.comwrote:
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.
Aug 24 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.