Hex to int conversion error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adam Ritter

    Hex to int conversion error

    When I try to convert an 8 digit hex number to an integer, I get a
    ValueError. Why doesn't it convert back correctly? I have the string
    '0xdeadbeaf' stored in a textbox and I would like it's integer value. I
    would convert it to a long, but I need to pack it to send as a 4 byte
    integer through a socket to a C program. Any ideas?
    [color=blue][color=green][color=darkred]
    >>>int(0xdeadbe af)[/color][/color][/color]
    -559038801[color=blue][color=green][color=darkred]
    >>>int(hex(int( 0xdeadbeaf)) ,16)[/color][/color][/color]
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    ValueError: int() literal too large: 0xdeadbeaf

    Nick

    _______________ _______________ _______________ _______________ _____
    Enjoy MSN 8 patented spam control and more with MSN 8 Dial-up Internet
    Service. Try it FREE for one month! http://join.msn.com/?page=dept/dialup


  • Peter Hansen

    #2
    Re: Hex to int conversion error

    Adam Ritter wrote:[color=blue]
    >
    > When I try to convert an 8 digit hex number to an integer, I get a
    > ValueError. Why doesn't it convert back correctly? I have the string
    > '0xdeadbeaf' stored in a textbox and I would like it's integer value. I
    > would convert it to a long, but I need to pack it to send as a 4 byte
    > integer through a socket to a C program. Any ideas?
    >[color=green][color=darkred]
    > >>>int(0xdeadbe af)[/color][/color]
    > -559038801[color=green][color=darkred]
    > >>>int(hex(int( 0xdeadbeaf)) ,16)[/color][/color]
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > ValueError: int() literal too large: 0xdeadbeaf[/color]

    If your description above, that you "need to pack it to send as a
    4 byte integer", is correct, you should need only the struct module:

    struct.pack('L' , long('deadbeef' , 16))

    The value 0xdeadbeef is negative if treated as an int (since ints are
    signed in Python), so you can't treat it as an unsigned int. Instead,
    since in effect you want to treat all values as unsigned, use long()
    and the "L" (unsigned long) operand to struct.pack. Note that if you
    then give it a negative long, you'll still get an OverflowError,
    this time from struct.pack itself.

    -Peter

    Comment

    • John Roth

      #3
      Re: Hex to int conversion error


      "Adam Ritter" <temporary_addr @hotmail.com> wrote in message
      news:mailman.13 7.1067269508.70 2.python-list@python.org ...[color=blue]
      > When I try to convert an 8 digit hex number to an integer, I get a
      > ValueError. Why doesn't it convert back correctly? I have the string
      > '0xdeadbeaf' stored in a textbox and I would like it's integer value. I
      > would convert it to a long, but I need to pack it to send as a 4 byte
      > integer through a socket to a C program. Any ideas?
      >[color=green][color=darkred]
      > >>>int(0xdeadbe af)[/color][/color]
      > -559038801[color=green][color=darkred]
      > >>>int(hex(int( 0xdeadbeaf)) ,16)[/color][/color]
      > Traceback (most recent call last):
      > File "<stdin>", line 1, in ?
      > ValueError: int() literal too large: 0xdeadbeaf
      >
      > Nick[/color]

      Please see PEP 237. If the timeline in that PEP is still valid,
      the meaning will change in Release 2.4.

      John Roth


      Comment

      • Patrick Maupin

        #4
        Re: Hex to int conversion error

        "Adam Ritter" <temporary_addr @hotmail.com> wrote in message news:<mailman.1 37.1067269508.7 02.python-list@python.org >...[color=blue]
        > When I try to convert an 8 digit hex number to an integer, I get a
        > ValueError. Why doesn't it convert back correctly? I have the string
        > '0xdeadbeaf' stored in a textbox and I would like it's integer value. I
        > would convert it to a long, but I need to pack it to send as a 4 byte
        > integer through a socket to a C program. Any ideas?
        >[color=green][color=darkred]
        > >>>int(0xdeadbe af)[/color][/color]
        > -559038801[color=green][color=darkred]
        > >>>int(hex(int( 0xdeadbeaf)) ,16)[/color][/color]
        > Traceback (most recent call last):
        > File "<stdin>", line 1, in ?
        > ValueError: int() literal too large: 0xdeadbeaf[/color]

        Unfortunately, that's what you get in 2.2, which is _different_
        than what you'll get in 2.3, which is _still_ _different_ than
        what you'll get in 2.4. This whole "deal with machine words"
        is not really directly supported -- even using struct as
        one previous poster suggested can lead to trouble.

        The solution is to write your own 'hex' and 'int' functions.
        The following code **should** work under 2.2, 2.3, and 2.4,
        although it will give FutureWarnings under 2.3 (you can shut
        them off using the filter in the warnings module).

        import sys


        def short(what,offs et=sys.maxint+1 ,modulus=(sys.m axint+1)*2):
        """short(n) converts a long into an int, ignoring high-order bits"""
        return int(((what + offset) % modulus) - offset)

        def poslong(what,mo dulus=(sys.maxi nt+1)*2):
        """poslong( n) takes an int and returns a long with the same bit
        pattern in the lower bits, and zeros in the upper bits. (Guaranteed
        positive result)"""
        return what % modulus

        def hexshort(what):
        """hexshort (n) returns a hex number without any annoying minus sign in 2.4"""
        return '0x%x' % poslong(what)

        print short(0xdeadbee f)
        print hexshort(short( 0xdeadbeef))
        print long(hexshort(s hort(0xdeadbeef )),16)
        print short(long(hexs hort(short(0xde adbeef)),16))

        Hope this helps.

        Pat

        Comment

        • Patrick Maupin

          #5
          Re: Hex to int conversion error

          "Adam Ritter" <temporary_addr @hotmail.com> wrote in message news:<mailman.1 37.1067269508.7 02.python-list@python.org >...[color=blue]
          > When I try to convert an 8 digit hex number to an integer, I get a
          > ValueError. Why doesn't it convert back correctly? I have the string
          > '0xdeadbeaf' stored in a textbox and I would like it's integer value. I
          > would convert it to a long, but I need to pack it to send as a 4 byte
          > integer through a socket to a C program. Any ideas?
          >[color=green][color=darkred]
          > >>>int(0xdeadbe af)[/color][/color]
          > -559038801[color=green][color=darkred]
          > >>>int(hex(int( 0xdeadbeaf)) ,16)[/color][/color]
          > Traceback (most recent call last):
          > File "<stdin>", line 1, in ?
          > ValueError: int() literal too large: 0xdeadbeaf[/color]


          In my previous post, I forgot to mention that the code sample
          I gave only gives FutureWarnings because of the literal hex
          constant used. In your real application, you will not need
          to do this (because you will be using long(somestring ,16),
          similar to the third test line in my example). So the chances
          are that you will not encounter any FutureWarnings using this
          method.

          Pat

          Comment

          Working...