Velocity Reviews > signed vs unsigned int

# signed vs unsigned int

johnty
Guest
Posts: n/a

 06-02-2010
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[i]). however, what i'd like is to get the _signed_
integer value. whats the easiest way to do this?

johnty

Stefan Behnel
Guest
Posts: n/a

 06-02-2010
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[i]). 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

Steven D'Aprano
Guest
Posts: n/a

 06-02-2010
On Tue, 01 Jun 2010 23:43:33 -0700, johnty wrote:

> 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[i]). however, what i'd like is to get the _signed_
> integer value. whats the easiest way to do this?

>>> import array
>>> s = 'Some unsigned bytes \xc3\x80\xc3\xa0\xc3\xa6\xc3\x9f\xc2\xb5'
>>> array.array('b', s)

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])

--
Steven

johnty
Guest
Posts: n/a

 06-02-2010
On Jun 2, 12:04*am, Christian Heimes <(E-Mail Removed)> wrote:
> > 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[i]). 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!

John Machin
Guest
Posts: n/a

 06-02-2010
On Jun 2, 4:43*pm, johnty <(E-Mail Removed)> wrote:
> 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[i]). 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

Patrick Maupin
Guest
Posts: n/a

 06-04-2010
On Jun 2, 6:25*am, John Machin <(E-Mail Removed)> wrote:
> On Jun 2, 4:43*pm, johnty <(E-Mail Removed)> wrote:
>
> > 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[i]). 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 & 12
signed = (unsigned & 127) * 2 - unsigned
signed - unsigned - 2 * (unsigned & 12

Regards,
Pat

John Nagle
Guest
Posts: n/a

 06-04-2010
johnty wrote:
> 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

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post pozz C Programming 12 03-20-2011 11:32 PM Fore C++ 29 09-21-2008 05:55 AM Alex C Programming 3 04-26-2006 05:20 AM G Fernandes C Programming 2 02-16-2005 10:55 AM Siemel Naran C++ 3 11-29-2004 08:22 AM