two's complement bytes

A

Adam W.

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?
 
C

castironpi

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
 
A

Adam W.

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
 
C

castironpi

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
 
A

Adam W.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top