Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #20566

Re: signed to unsigned

From Peter Otten <__peter__@web.de>
Subject Re: signed to unsigned
Date 2012-02-17 20:14 +0100
Organization None
References <436b024b-20f7-462b-a21e-11f44802e578@s7g2000vby.googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.5923.1329506097.27778.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

signed to unsigned Brad Tilley <kj4eit@gmail.com> - 2012-02-17 10:51 -0800
  Re: signed to unsigned Chris Rebert <clp2@rebertia.com> - 2012-02-17 11:05 -0800
    Re: signed to unsigned Brad Tilley <kj4eit@gmail.com> - 2012-02-17 11:10 -0800
  Re: signed to unsigned Peter Otten <__peter__@web.de> - 2012-02-17 20:14 +0100
    Re: signed to unsigned Brad Tilley <kj4eit@gmail.com> - 2012-02-17 11:22 -0800
      Re: signed to unsigned Dave Angel <d@davea.name> - 2012-02-17 14:37 -0500

csiph-web