How to convert a number to hex number?

Steve Holden steve at holdenweb.com
Tue Nov 8 16:11:14 EST 2005


Paul Rubin wrote:
> "dcrespo" <dcrespo at gmail.com> writes:
> 
>>>>>hex(255)[2:]
>>
>>'ff'
> 
> 
> '%x'%255 is preferable since the format of hex() output can vary.  Try hex(33**33).

?

You're usually smarter than this, or am I missing some joke?

  >>> hex(33*33)
'0x441'
  >>> hex(33*33)[2:]
'441'
  >>> 33*33
1089
  >>> "%x" % 1089
'441'
  >>>

Of course, you have to watch out for operator precedence:

  >>> "%x" % 33*33
'212121212121212121212121212121212121212121212121212121212121212121'

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                  www.python.org/pycon/




More information about the Python-list mailing list