Welcome, guest | Sign In | My Account | Store | Cart

Converts a decimal to it's hexadecimal equivalent

Python, 6 lines
1
2
3
4
5
6
def main():  
    user_input=raw_input("Please enter a number\n")  
    val = int(user_input)  
    print "The decimal %d is %X in hexadecimal.\n"%(val, val)  

main()

This is Akira Fora's rewrite

5 comments

Akira Fora 14 years, 11 months ago  # | flag

OMG!

def main():  
    user_input=raw_input("Please enter a number\n")  
    val = int(user_input)  
    print "The decimal %d is %X in hexadecimal.\n"%(val, val)  

main()
Kevin King 14 years, 11 months ago  # | flag

Akira, in the author's defense your example doesn't teach you to understand hexadecimal. Still, OMG is right!

def main():
    digits=["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"]

    user_input=raw_input("Please enter a number\n")
    val = int(user_input)

    while val > 0:
        print digits[val % len(digits)],
        val = int(val / len(digits))

Coincidentally works with any base. Just specify the digits and you're done.

wolf550e 14 years, 11 months ago  # | flag
  1. "digits" can be a string, strings can be accessed as arrays of characters.

  2. Integer division has an operator ("//"), but even better is to use the built-in function divmod.

  3. It is traditional to write numbers from left to right, even in hex, so reverse the output digits.

  4. In the common case of the base being a power of 2, depending on how numbers are represented, it is faster to use binary AND for remainder and a right shift for division.

    def main():

    digits="0123456789ABCDEF"
    
    user_input=raw_input("Please enter a number\n")
    val = int(user_input)
    
    a = []
    while val > 0:
        val, remainder = divmod(val, len(digits))
        a.append(digits[remainder])
    
    print "".join(reversed(a))
    
Martin Schimmels 14 years, 8 months ago  # | flag

A generalisation:

decNumber = input("Number in decimal system: ") numBase = input("Base of number system, to which number shall be transformed (<= 24): ") if numBase > 10: moreThanDec = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L', 'M', 'N']

listOfRemainders = [] while(decNumber > 0): (decNumber,remainder) = divmod(decNumber, numBase) if numBase > 10: remainderAlpha = moreThanDec[remainder] listOfRemainders.append(remainderAlpha) else: listOfRemainders.append(str(remainder))

listOfRemainders.reverse() for i in listOfRemainders: print i,

Martin Schimmels 14 years, 8 months ago  # | flag

A generalisation: (sorry for the missing formatting before)

decNumber = input("Number in decimal system: ")
numBase = input("Base of number system, to which number shall be transformed (<= 24): ")
if numBase > 10:
    moreThanDec = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L', 'M',  'N']

listOfRemainders = []
while(decNumber > 0):
    (decNumber,remainder) = divmod(decNumber, numBase)
    if numBase > 10:
        remainderAlpha = moreThanDec[remainder]
        listOfRemainders.append(remainderAlpha)
    else:
        listOfRemainders.append(str(remainder))

listOfRemainders.reverse()
for i in listOfRemainders:
    print i,
Created by joedaviscpa on Tue, 19 May 2009 (MIT)
Python recipes (4591)
joedaviscpa's recipes (3)

Required Modules

  • (none specified)

Other Information and Tasks