Converting a hex string to an integer

Peter Koren pkoren at hex.net
Thu Mar 30 21:46:21 EST 2000


Peter Koren wrote:
> 
> Vijay Baliga wrote:
> >
> > How can I convert a hex string like '0xab' to an integer? Here is what
> > I found:
> > >>> int('43')
> > 43
> > >>> hex(171)
> > '0xab'
> > >>> int('0xab')
> > Traceback (innermost last):
> > File "<stdin>", line 1, in ?
> > ValueError: invalid literal for int(): 0xab
> > Thanks for any help,
> > Vijay
> >
> 
> >>> def printint(x):
> ...     print x
> ...
> >>> printint(0xab)
> 171
> >>>
> 
> Regards,
> 
> Peter Koren

Sorry, that is not so useful as:

>>> def printint(x):
...  return x
...
>>> printint(0xab)
171
>>> printint(0xab)*2
342
>>> printint(0xab)/2.5
68.4

and so on. 

Regards,

Peter Koren



More information about the Python-list mailing list