Hex to int conversion error

Discussion in 'Python' started by Adam Ritter, Oct 27, 2003.

  1. Adam Ritter

    Adam Ritter Guest

    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?

    >>>int(0xdeadbeaf)

    -559038801
    >>>int(hex(int(0xdeadbeaf)) ,16)

    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
     
    Adam Ritter, Oct 27, 2003
    #1
    1. Advertising

  2. Adam Ritter

    Peter Hansen Guest

    Adam Ritter wrote:
    >
    > 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?
    >
    > >>>int(0xdeadbeaf)

    > -559038801
    > >>>int(hex(int(0xdeadbeaf)) ,16)

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > ValueError: int() literal too large: 0xdeadbeaf


    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
     
    Peter Hansen, Oct 27, 2003
    #2
    1. Advertising

  3. Adam Ritter

    John Roth Guest

    "Adam Ritter" <> wrote in message
    news:...
    > 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?
    >
    > >>>int(0xdeadbeaf)

    > -559038801
    > >>>int(hex(int(0xdeadbeaf)) ,16)

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > ValueError: int() literal too large: 0xdeadbeaf
    >
    > Nick


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

    John Roth
     
    John Roth, Oct 27, 2003
    #3
  4. "Adam Ritter" <> wrote in message news:<>...
    > 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?
    >
    > >>>int(0xdeadbeaf)

    > -559038801
    > >>>int(hex(int(0xdeadbeaf)) ,16)

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > ValueError: int() literal too large: 0xdeadbeaf


    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,offset=sys.maxint+1,modulus=(sys.maxint+1)*2):
    """short(n) converts a long into an int, ignoring high-order bits"""
    return int(((what + offset) % modulus) - offset)

    def poslong(what,modulus=(sys.maxint+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(0xdeadbeef)
    print hexshort(short(0xdeadbeef))
    print long(hexshort(short(0xdeadbeef)),16)
    print short(long(hexshort(short(0xdeadbeef)),16))

    Hope this helps.

    Pat
     
    Patrick Maupin, Oct 28, 2003
    #4
  5. "Adam Ritter" <> wrote in message news:<>...
    > 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?
    >
    > >>>int(0xdeadbeaf)

    > -559038801
    > >>>int(hex(int(0xdeadbeaf)) ,16)

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > ValueError: int() literal too large: 0xdeadbeaf



    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
     
    Patrick Maupin, Oct 28, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,217
    Neredbojias
    Aug 19, 2005
  2. Schnoffos
    Replies:
    2
    Views:
    1,220
    Martien Verbruggen
    Jun 27, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,646
    Old Wolf
    Jan 20, 2004
  4. Bengt Richter
    Replies:
    6
    Views:
    471
    Juha Autero
    Aug 19, 2003
  5. Batista, Facundo

    RE: Hex to int conversion error

    Batista, Facundo, Oct 27, 2003, in forum: Python
    Replies:
    0
    Views:
    386
    Batista, Facundo
    Oct 27, 2003
Loading...

Share This Page