Re: signed to unsigned

2012-02-17 Thread Dave Angel
On 02/17/2012 02:22 PM, Brad Tilley wrote: 0x& -327681234 3967286062 Very nice! Thanks for that example. Unsigned long longs: 0x& -9151314442815602945 9295429630893948671L Or more generally, use modulo -13452324 % 2^64 -- DaveA -- http://mail.python.org/mailman/l

Re: signed to unsigned

2012-02-17 Thread Brad Tilley
> >>> 0x & -327681234 > > 3967286062 Very nice! Thanks for that example. Unsigned long longs: 0x & -9151314442815602945 9295429630893948671L -- http://mail.python.org/mailman/listinfo/python-list

Re: signed to unsigned

2012-02-17 Thread Brad Tilley
> 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 wou

Re: signed to unsigned

2012-02-17 Thread Peter Otten
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(" Tra

Re: signed to unsigned

2012-02-17 Thread Chris Rebert
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 fail

signed to unsigned

2012-02-17 Thread Brad Tilley
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("", line 1, in struct.error: integer ou