converting hex number contained in a string to an integer

Roland Schlenker rol9999 at attglobal.net
Tue Jun 20 06:48:57 EDT 2000


ejfc at my-deja.com wrote:

> I have a string containing a number. This is an
> hex number. How can I convert this to an integer?
>
> >>> hexnumber
> '32'
> >>> 0x32
> 50
> >>> int(0x32)
> 50  # this is the result I want to get
> >>> hexnumber = '0x' + hexnumber
> >>> hexnumber
> '0x32'
> >>> int(hexnumber)
> Traceback (innermost last):
>   File "<interactive input>", line 1, in ?
> ValueError: invalid literal for int(): 0x32

>>> import string
>>> string.atoi(hexnumber, 16)
50

Roland Schlenker




More information about the Python-list mailing list