signed vs unsigned int

J

johnty

i'm reading bytes from a serial port, and storing it into an array.

each byte represents a signed 8-bit int.

currently, the code i'm looking at converts them to an unsigned int by
doing ord(array). however, what i'd like is to get the _signed_
integer value. whats the easiest way to do this?

thanks in advance.

johnty
 
S

Stefan Behnel

johnty, 02.06.2010 08:43:
i'm reading bytes from a serial port, and storing it into an array.

each byte represents a signed 8-bit int.

currently, the code i'm looking at converts them to an unsigned int by
doing ord(array). however, what i'd like is to get the _signed_
integer value. whats the easiest way to do this?


See the struct module, it supports various different C types.

Stefan
 
S

Steven D'Aprano

i'm reading bytes from a serial port, and storing it into an array.

An array or a list?

each byte represents a signed 8-bit int.

currently, the code i'm looking at converts them to an unsigned int by
doing ord(array). however, what i'd like is to get the _signed_
integer value. whats the easiest way to do this?

array('b', [83, 111, 109, 101, 32, 117, 110, 115, 105, 103, 110, 101,
100, 32, 98, 121, 116, 101, 115, 32, -61, -128, -61, -96, -61, -90, -61,
-97, -62, -75])


See also the fromstring method of array objects.
 
J

johnty

i'm reading bytes from a serial port, and storing it into an array.
each byte represents a signed 8-bit int.
currently, the code i'm looking at converts them to an unsigned int by
doing ord(array). however, what i'd like is to get the _signed_
integer value. whats the easiest way to do this?


http://docs.python.org/library/struct.html


the struct docs is exactly what i needed to read. "unpacking" it as a
signed char did the trick. thanks guys!
 
J

John Machin

i'm reading bytes from a serial port, and storing it into an array.

each byte represents a signed 8-bit int.

currently, the code i'm looking at converts them to an unsigned int by
doing ord(array). however, what i'd like is to get the _signed_
integer value. whats the easiest way to do this?


signed = unsigned if unsigned <= 127 else unsigned - 256
 
P

Patrick Maupin

i'm reading bytes from a serial port, and storing it into an array.
each byte represents a signed 8-bit int.
currently, the code i'm looking at converts them to an unsigned int by
doing ord(array). however, what i'd like is to get the _signed_
integer value. whats the easiest way to do this?


signed = unsigned if unsigned <= 127 else unsigned - 256


That works, but I prefer not using if/else for things that can be
described in an expression without it. Other ways of expressing this
include:

signed = (unsigned & 127) - (unsigned & 128)
signed = (unsigned & 127) * 2 - unsigned
signed - unsigned - 2 * (unsigned & 128)

Regards,
Pat
 
J

John Nagle

johnty said:
i'm reading bytes from a serial port, and storing it into an array.

Try reading into a type "bytearray". That's the proper data type
for raw bytes.

John Nagle
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top