Dealing with hex

M

Matt Gerrans

Is there an ideal approach to dealing with the FutureWarning about getting
the hex value of a negative int? I'm using zlib.crc32() which can return
a negative 32-bit int value, so for example print '%x' % zlib.crc32(
buffer ) generates the FutureWarning. I know I could jump through hoops
to coerce the int into a long that will have the same CRC, but is there some
particular recomendation for this (I looked at PEP 237, but there wasn't a
suggestion).
 
D

Derrick 'dman' Hudson

Is there an ideal approach to dealing with the FutureWarning about getting
the hex value of a negative int? I'm using zlib.crc32() which can return
a negative 32-bit int value, so for example print '%x' % zlib.crc32(
buffer ) generates the FutureWarning. I know I could jump through hoops
to coerce the int into a long that will have the same CRC, but is there some
particular recomendation for this (I looked at PEP 237, but there wasn't a
suggestion).

Hoops? For example :
print "%x" % long( zlib.crc32( buffer ) )

That seems simple enough to me.
 
D

Dan Bishop

Derrick 'dman' Hudson said:
Hoops? For example :
print "%x" % long( zlib.crc32( buffer ) )

That seems simple enough to me.

Or if you wanted the old semantics:

def unsigned(n):
return n & 0xFFFFFFFFL

print "%x" % unsigned(zlib.crc32(buffer))
 
M

Matt Gerrans

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

Dan said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,475
Latest member
ShannonGro

Latest Threads

Top