Dealing with hex

Matt Gerrans mgerrans at
Thu Dec 11 09:24:56 CET 2003

Derrick 'dman' Hudson wrote:
> > Hoops?  For example :
> >     print "%x" % long( zlib.crc32( buffer ) )
> >
> > That seems simple enough to me.

Simple indeed.  In fact it's so simple it doesn't work:

>>> print "%x" % long( zlib.crc32('test') )

Dan Bishop wrote:
> Or if you wanted the old semantics:
> def unsigned(n):
>    return n & 0xFFFFFFFFL
> print "%x" % unsigned(zlib.crc32(buffer))

This avoids the FutureWarning, but the PEP mentions that the L-suffix
notation will also be phased out, so I wanted to avoid that in the solution
and that can be done like this:

def unsigned32(n): return n & 4294967295

since the large decimal of 0xffffffff value will automatically be a long.

It seems kind of clunky to have to throw this extra function call into the
mix whenever dealing with 32-bit values.    I don't think 32-bit values will
be made obsolete by 64-bit systems for quite a while to come.  This
particular update to Python seems a little pie-in-the-sky and un-pragmatic.

More information about the Python-list mailing list