Conversion to Hex or other bases

Remco Gerlich scarblac-spamtrap at pino.selwerd.nl
Sun Mar 19 23:41:58 CET 2000


Gerhard Gruber wrote in comp.lang.python:
> How can I convert a decimal to another base? i.e. Hex or binary?

This question has been asked several times in the last few days.
I'll just give a quick answer:

"%o" % 31
Gives '37', 31 in octal.

"%x" % 31
Gives '1f', 31 in hexadecimal.

There isn't any other way for other bases that I can think of (well, apart
from str(x) to get the decimal :-)). You need to roll your own or look at
the recent posts on this subject for a function that handles any base
up to 36 (10 digits + 26 characters).

-- 
Remco Gerlich,  scarblac at pino.selwerd.nl

   This is no way to be
     Man ought to be free      -- Ted Bundy
       That man should be me



More information about the Python-list mailing list