ActiveState Code

Recipe 576766: Hexadecimal Conversion Tool


Converts a decimal to it's hexadecimal equivalent

Python
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()

Discussion

This is Akira Fora's rewrite

Comments

  1. 1. At 1:16 a.m. on 20 may 2009, Akira Fora said:

    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()
    
  2. 2. At 10:07 a.m. on 20 may 2009, Kevin King said:

    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.

  3. 3. At 3:41 p.m. on 20 may 2009, wolf550e said:
    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))
      
  4. 4. At 6:18 a.m. on 10 aug 2009, Martin Schimmels said:

    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,

  5. 5. At 6:26 a.m. on 10 aug 2009, Martin Schimmels said:

    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,
    

Sign in to comment