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


Groups > comp.lang.python > #20562 > unrolled thread

signed to unsigned

Started byBrad Tilley <kj4eit@gmail.com>
First post2012-02-17 10:51 -0800
Last post2012-02-17 14:37 -0500
Articles 6 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  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

#20562 — signed to unsigned

FromBrad Tilley <kj4eit@gmail.com>
Date2012-02-17 10:51 -0800
Subjectsigned to unsigned
Message-ID<436b024b-20f7-462b-a21e-11f44802e578@s7g2000vby.googlegroups.com>
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.

[toc] | [next] | [standalone]


#20564

FromChris Rebert <clp2@rebertia.com>
Date2012-02-17 11:05 -0800
Message-ID<mailman.5922.1329505526.27778.python-list@python.org>
In reply to#20562
On Fri, Feb 17, 2012 at 10:51 AM, Brad Tilley <kj4eit@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#20565

FromBrad Tilley <kj4eit@gmail.com>
Date2012-02-17 11:10 -0800
Message-ID<34af3238-9541-48bf-b1a3-bd1e07f1fbc5@w1g2000vbg.googlegroups.com>
In reply to#20564
> 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

[toc] | [prev] | [next] | [standalone]


#20566

FromPeter Otten <__peter__@web.de>
Date2012-02-17 20:14 +0100
Message-ID<mailman.5923.1329506097.27778.python-list@python.org>
In reply to#20562
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

[toc] | [prev] | [next] | [standalone]


#20567

FromBrad Tilley <kj4eit@gmail.com>
Date2012-02-17 11:22 -0800
Message-ID<17514709-e8bc-4533-b9db-5cf9b27c4c90@m2g2000vbc.googlegroups.com>
In reply to#20566
> >>> 0xffffffff & -327681234
>
> 3967286062

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

0xffffffffffffffff & -9151314442815602945
9295429630893948671L

[toc] | [prev] | [next] | [standalone]


#20568

FromDave Angel <d@davea.name>
Date2012-02-17 14:37 -0500
Message-ID<mailman.5924.1329507479.27778.python-list@python.org>
In reply to#20567
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web