Dealing with hex

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matt Gerrans

    Dealing with hex

    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).


  • Derrick 'dman' Hudson

    #2
    Re: Dealing with hex

    On Wed, 10 Dec 2003 03:49:16 GMT, Matt Gerrans wrote:[color=blue]
    > 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).[/color]

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

    That seems simple enough to me.

    --
    If your life is a hard drive,
    Christ can be your backup.

    www: http://dman13.dyndns.org/~dman/ jabber: dman@dman13.dyn dns.org

    Comment

    • Dan Bishop

      #3
      Re: Dealing with hex

      Derrick 'dman' Hudson <dman@dman13.dy ndns.org> wrote in message news:<6clka1-ksi.ln1@dman13. dyndns.org>...[color=blue]
      > On Wed, 10 Dec 2003 03:49:16 GMT, Matt Gerrans wrote:[color=green]
      > > 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).[/color]
      >
      > Hoops? For example :
      > print "%x" % long( zlib.crc32( buffer ) )
      >
      > That seems simple enough to me.[/color]

      Or if you wanted the old semantics:

      def unsigned(n):
      return n & 0xFFFFFFFFL

      print "%x" % unsigned(zlib.c rc32(buffer))

      Comment

      • Matt Gerrans

        #4
        Re: Dealing with hex

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

        Simple indeed. In fact it's so simple it doesn't work:
        [color=blue][color=green][color=darkred]
        >>> print "%x" % long( zlib.crc32('tes t') )[/color][/color][/color]
        -278081f4

        Dan Bishop wrote:[color=blue]
        > Or if you wanted the old semantics:
        >
        > def unsigned(n):
        > return n & 0xFFFFFFFFL
        >
        > print "%x" % unsigned(zlib.c rc32(buffer))[/color]

        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.


        Comment

        Working...