0 down vote favorite

I have written a code to convert decimal numbers into hexadecimal equivalent. The program prints correct value till '2559'. How to get correct values of hexadecimal for larger numbers. Here is my code:

#########################################
#   Decimal to Hexadecimal Conversion   #
#########################################


def DectoHex(n):

    if n <= 16:
        return n

    elif n>16:          

        if n%16 == 10:
            x = 'A'
        elif n%16 == 11:
            x = 'B'
        elif n%16 == 12:
            x = 'C'
        elif n%16 == 13:
            x = 'D'
        elif n%16 == 14:
            x ='E'
        elif n%16 == 15:
            x = 'F'
        else:
            x = n%16            
        print x     
        n = n/16        
        print n

        if n == 10:
            n = 'A'
        elif n == 11:
            n = 'B'
        elif n == 12:
            n = 'C'
        elif n == 13:
            n = 'D'
        elif n == 14:
            n ='E'
        elif n == 15:
            n = 'F'

        elif n>=16:

            if n%16 == 10:
                n = str(n/16) + 'A'
            elif n%16 == 11:
                n = str(n/16) + 'B'
            elif n%16 == 12:
                n = str(n/16) + 'C'
            elif n%16 == 13:
                n = str(n/16) + 'D'
            elif n%16 == 14:
                n = str(n/16) + 'E'
            elif n%16 == 15:
                n = str(n/16) + 'F'
            else:
                n = str(n/16) + str(n%16)
            print n 
        return str(n) + str(x)


print "Would you like to continue:"
print "Enter 'Y' to continue, 'N' to quit"
Str = str (raw_input("> "))

while True:

        if Str == 'Y':
            print "Enter a decimal number:"
            dec = int (raw_input("> "))

            Hex = DectoHex(dec)

            print "The number in base 16 is:", Hex

            print "Enter 'Y' to continue, 'N' to quit"
            Str = str (raw_input("> "))

        elif Str == 'N':
            print "Good Bye!"
            break
        else:
            print "Plesae Enter 'Y' or 'N'"
            Str = str (raw_input("> "))

If you want to handle larger numbers, you must add a while loop which runs as long as the number is not zero. Here is a way to do it

HEX_DIGITS = "0123456789ABCDEF"

def hex_digit(n):
    """Compute the hexadecimal digit for a number n in [0, 16[.

        >>> hex_digit(7)
        '7'
        >>> hex_digit(12)
        'C'
        >>> hex_digit(20)
        Traceback (most recent call last):
            ...
    """
    assert 0 <= n < 16
    return HEX_DIGITS[n]

def dec_to_hex(n):
    """Compute the hexadecimal representation of a non negative integer""" 
    assert n >= 0
    if n == 0:
        return '0'
    result = ''
    while n:
        n, rest = divmod(n, 16)
        result = hex_digit(rest) + result
    return result

Notice that the standard library contains a function hex() which does exactly this. Try hex(n)[2:].upper().

For a better style, use documentation strings in your functions, and also use assert statements to check things that look obvious when you're writing the code but which may fail when the code is used (like assert n >= 0).

Edited 4 Years Ago by Gribouillis

Thanks for the response. Yes, i have used the same technique which you have mentioned with the standard library, was trying to implement this function. Earlier, the function worked till 400, then i tweaked it which worked till 2559.

Edited 4 Years Ago by peterparker

This article has been dead for over six months. Start a new discussion instead.