shiv
shiv

Reputation: 1

Converting Decimal to Hexadecimal in python

  1. i am getting answer as 7D while i should be getting 7D0. I actually dont know how to solve it:
  2. my test case at the beginning was 31 which answer was correct 1F but as i go towards higher number my code doesnt work.
  3. using loops might be more efficient but i am not too comfortable with loop right now
  4. please help !!!

    def dec2hex(n):
    x1 =0
    counter = 0 
    answer = ""
    if n<=0:
        answer =answer + "0"
    else:
        while (n >16):
            counter +=1
            n  =  n /16
    
            x = n
    
            if(x <16 ):
                x = int(n)
                break
            else: 
                continue
        if ((n-x) *16 <16 ):
            counter1 = 1
        else:
            counter1 = counter -1
        rem = (n-x) * (16**(counter1))
    
        if rem >16:
            while (n >16):
                rem = rem /16
    
                x1 = rem
    
                if(x1 <16 ):
                    x1 = int(rem)
                    break
                else: 
                    continue
        if n < 10:
            answer =answer + str(int(x))
        if (rem  ==10 or x1 ==10):
            answer = answer + "A"
        if (rem  ==11 or x1 ==11):
            answer = answer + "B"
        if (rem ==12 or x1 ==12):
            answer = answer + "C"
        if (rem  ==13 or x1 ==13):
            answer = answer + "D"
        if (rem  ==14 or x1 ==14):
            answer = answer + "E"
        if (rem  ==15 or x1 ==15):
            answer = answer + "F"
        print(counter,rem,x1,n,counter,x)
    return answer
    

    dec2hex(2000)

Upvotes: 0

Views: 4005

Answers (2)

Pyroseza
Pyroseza

Reputation: 131

The most elegant answer I can think of is to format a string literal and it will convert the value for you

3.6+ (f-strings)

>>> d = 2000
>>> print(f'{d:X}')
7D0

pre-3.6 (string format function)

>>> d = 2000
>>> print('{:X}'.format(d))
7D0

https://docs.python.org/3/reference/lexical_analysis.html#f-strings

https://docs.python.org/3/library/string.html#formatspec

Upvotes: 1

AndroX
AndroX

Reputation: 103

I would like to propose this approach to the problem. A while loop is used to cycle the main division and the reminder calculus A for loop is used to invert the final answer string

The while loop condition exclude the case in which 0 is entered becasue the division remainder will be always zero and the loop will be infinite. In this case the answer will be forced to 0 by the first if.

def dec2hex(hexnum):
hexstring = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"]
counter = 0
remainder = []
answer = ""
if hexnum > 0:
    while hexnum > 0:
        remainder.append(hexnum % 16)
        hexnum = hexnum // 16
        counter = counter + 1
    for reverse in remainder[::-1]:
        answer = answer + hexstring[reverse]
else:
    answer = "0"
return answer

print(dec2hex(2000))

Upvotes: 0

Related Questions