[Tutor] convert decimal to hexa, to octal and vice versa.

Alan Gauld ukc802591034 at btconnect.com
Wed Apr 5 13:48:08 CEST 2006

"Keo Sophon" <keosophon at khmeros.info> wrote in message 
news:200604051832.56959.keosophon at khmeros.info...

> How to convert a decimal number to hexadecimal, octal and vice versa?

The number is stored in binary on the computer so you never convert
the number itself, what you convert is the representation of that number
as a string.

The easiest way (and the one with most control) is to use string formatting:

number = 42
print "%X" % number
print "%x" % number
print "%05x" % number
print "%o" % number


Alan G
Author of the learn to program web tutor

More information about the Tutor mailing list