Hex to int conversion error

Patrick Maupin pmaupin at speakeasy.net
Tue Oct 28 01:16:12 EST 2003


"Adam Ritter" <temporary_addr at hotmail.com> wrote in message news:<mailman.137.1067269508.702.python-list at python.org>...
> 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




More information about the Python-list mailing list