[Tutor] Covert numbers to hex fails

ZIYAD A. M. AL-BATLY zamb at saudi.net.sa
Wed May 25 05:25:49 CEST 2005


On Tue, 2005-05-24 at 22:56 -0400, Tom Tucker wrote:
> Good evening!  I am trying to pass a number variable and have it
> converted to hex.  Any recommendations on how to achieve this?  Thank
> you.
> 
> FAILS
> ----------
> >>> value = 1234567890
> >>> hexoutput = hex('%d' % (value))
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: hex() argument can't be converted to hex
> >>> 
> 
> 
> 
> WORKS
> -------------
> >>> hexoutput = hex(1234567890)
> >>> print hexoutput
> 0x499602d2
> >>>
Try one of those:
        '%x' % value # Will output the hex value without the leading 0x
        hex(value)   # Leading 0x will be printed

Ziyad.


More information about the Tutor mailing list