We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,617 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Decimal To Hexadecimal in Python for large numbers

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("> "))
2
Contributors
2
Replies
42 Minutes
Discussion Span
10 Months Ago
Last Updated
4
Views
peterparker
Light Poster
32 posts since Jul 2012
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0

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).

Gribouillis
Posting Maven
Moderator
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11

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.

peterparker
Light Poster
32 posts since Jul 2012
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0600 seconds using 2.64MB