[Tutor] Unsigned numerical literals?

dman dsh8290@rit.edu
Fri, 21 Dec 2001 12:54:41 -0500


On Wed, Dec 19, 2001 at 03:54:47PM -0500, Lee-Shanok, Bruce wrote:
| Hello all. Quick question: I'm writing a wrapper for some C calls in Python.
| The issue here is that the C code takes an unsigned long. Now, Python
| integers are, to the best of my understanding, also 32 bit, but not unsigned
| by default. Is there a clean way to force them to be unsigned? I'd hate to
| have to force the users of the Python code to write an "L" at the end of
| every value they bring in....

How about :

>>> import struct
>>> s = struct.pack( "L" , 123 )
>>> i = struct.unpack( "L" , s )
>>> print i
(123L,)

The struct module allows you to treat a string as a bunch of binary
data.  It may be helpful for you to use it to turn the python objects
into binary data.  You may want to just convert the numbers to longs
and ensure the value is positive.

-D

-- 

the nice thing about windoze is - it does not just crash,
it displays a dialog box and lets you press 'ok' first.