signed to unsigned

Discussion in 'Python' started by Brad Tilley, Feb 17, 2012.

  1. Brad Tilley

    Brad Tilley Guest

    In C or C++, I can do this for integer conversion:

    unsigned int j = -327681234; // Notice this is signed.

    j will equal 3967286062. I thought with Python that I could use struct
    to pack the signed int as an unsigned int, but that fails:

    >>> x = struct.pack("<I", -327681234)

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    struct.error: integer out of range for 'I' format code

    Is there an easy way in Python to do the same conversion that C or C++
    code does? Thanks for any advice.
     
    Brad Tilley, Feb 17, 2012
    #1
    1. Advertising

  2. Brad Tilley

    Chris Rebert Guest

    On Fri, Feb 17, 2012 at 10:51 AM, Brad Tilley <> wrote:
    > In C or C++, I can do this for integer conversion:
    >
    > unsigned int j = -327681234; // Notice this is signed.
    >
    > j will equal 3967286062. I thought with Python that I could use struct
    > to pack the signed int as an unsigned int, but that fails:
    >
    >>>> x = struct.pack("<I", -327681234)

    > Traceback (most recent call last):
    >  File "<stdin>", line 1, in <module>
    > struct.error: integer out of range for 'I' format code
    >
    > Is there an easy way in Python to do the same conversion that C or C++
    > code does? Thanks for any advice.


    Pack it as the actual type, then unpack it as the desired type:

    Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from struct import pack, unpack
    >>> unpack('=I', pack('=i',-327681234))

    (3967286062,)

    I would think there's some more efficient way to do this though.

    Cheers,
    Chris
     
    Chris Rebert, Feb 17, 2012
    #2
    1. Advertising

  3. Brad Tilley

    Brad Tilley Guest

    > Pack it as the actual type, then unpack it as the desired type:
    >
    > Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
    > Type "help", "copyright", "credits" or "license" for more information.>>> from struct import pack, unpack
    > >>> unpack('=I', pack('=i',-327681234))

    >
    > (3967286062,)
    >
    > I would think there's some more efficient way to do this though.
    >
    > Cheers,
    > Chris



    Thanks Chris! I was doing it backwards. I only have a few of these
    right now, so performance isn't a concern. I appreciate the advice.

    Brad
     
    Brad Tilley, Feb 17, 2012
    #3
  4. Brad Tilley

    Peter Otten Guest

    Brad Tilley wrote:

    > In C or C++, I can do this for integer conversion:
    >
    > unsigned int j = -327681234; // Notice this is signed.
    >
    > j will equal 3967286062. I thought with Python that I could use struct
    > to pack the signed int as an unsigned int, but that fails:
    >
    >>>> x = struct.pack("<I", -327681234)

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    > struct.error: integer out of range for 'I' format code
    >
    > Is there an easy way in Python to do the same conversion that C or C++
    > code does? Thanks for any advice.


    >>> 0xffffffff & -327681234

    3967286062
     
    Peter Otten, Feb 17, 2012
    #4
  5. Brad Tilley

    Brad Tilley Guest


    > >>> 0xffffffff & -327681234

    >
    > 3967286062


    Very nice! Thanks for that example. Unsigned long longs:

    0xffffffffffffffff & -9151314442815602945
    9295429630893948671L
     
    Brad Tilley, Feb 17, 2012
    #5
  6. Brad Tilley

    Dave Angel Guest

    On 02/17/2012 02:22 PM, Brad Tilley wrote:
    >>>>> 0xffffffff& -327681234

    >> 3967286062

    > Very nice! Thanks for that example. Unsigned long longs:
    >
    > 0xffffffffffffffff& -9151314442815602945
    > 9295429630893948671L

    Or more generally, use modulo

    -13452324 % 2^64

    --

    DaveA
     
    Dave Angel, Feb 17, 2012
    #6
    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. Jeremy Pyle
    Replies:
    3
    Views:
    53,281
    Mike Treseler
    Jun 28, 2003
  2. Patrick

    signed to unsigned

    Patrick, Jun 7, 2004, in forum: VHDL
    Replies:
    1
    Views:
    5,644
    Ralf Hildebrandt
    Jun 7, 2004
  3. kyrpa83
    Replies:
    1
    Views:
    658
    kyrpa83
    Oct 17, 2007
  4. Rob1bureau
    Replies:
    1
    Views:
    843
    joris
    Feb 27, 2010
  5. pozz
    Replies:
    12
    Views:
    796
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page