signed vs unsigned int

Discussion in 'Python' started by johnty, Jun 2, 2010.

  1. johnty

    johnty Guest

    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
    johnty, Jun 2, 2010
    #1
    1. Advertising

  2. 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
    Stefan Behnel, Jun 2, 2010
    #2
    1. Advertising

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


    See also the fromstring method of array objects.


    --
    Steven
    Steven D'Aprano, Jun 2, 2010
    #3
  4. johnty

    johnty Guest

    On Jun 2, 12:04 am, Christian Heimes <> 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). 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!
    johnty, Jun 2, 2010
    #4
  5. johnty

    John Machin Guest

    On Jun 2, 4:43 pm, johnty <> 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). 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
    John Machin, Jun 2, 2010
    #5
  6. On Jun 2, 6:25 am, John Machin <> wrote:
    > On Jun 2, 4:43 pm, johnty <> 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). 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
    Patrick Maupin, Jun 4, 2010
    #6
  7. johnty

    John Nagle Guest

    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
    John Nagle, Jun 4, 2010
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Siemel Naran

    convert signed int to unsigned int

    Siemel Naran, Nov 28, 2004, in forum: C++
    Replies:
    3
    Views:
    34,050
    Ioannis Vranos
    Nov 29, 2004
  2. G Fernandes
    Replies:
    2
    Views:
    480
    Lawrence Kirby
    Feb 16, 2005
  3. Alex
    Replies:
    3
    Views:
    638
    Michael Mair
    Apr 26, 2006
  4. Fore
    Replies:
    29
    Views:
    15,175
    Rashad
    Sep 21, 2008
  5. pozz
    Replies:
    12
    Views:
    718
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page