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

5
  • 2
    If this isn't an exercise, you'd simply want return f"{n:X}" Commented Feb 14, 2018 at 0:33
  • Note, you are using loops. The simplest way would be to accumulate the hexidecimal digits (strings) in a list. Start by successively taking the remainder and appending the corresponding hexi-digit (you could use a list for this). Then, reverse the final result and ''.join itno a string. You really only need a single while-loop Commented Feb 14, 2018 at 0:40
  • 1
    Note, to get the remainder simply use the modulus operator, i.e. %, no need for this: rem = (n-x) * (16**(counter1)) you don't need a counter. So, simply put, you could do, n, rem = n // 16, n % 16, or use the helper function divmod, so n, rem = divmod(n, 16) Commented Feb 14, 2018 at 0:41
  • I am sorry i just noticed the mistake in the heading, i need to convert decimal to Hexadecimal no the other way around @juanpa.arrivillaga Commented Feb 14, 2018 at 0:54
  • Yes, that is what I had understood anyway. Commented Feb 14, 2018 at 0:55

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.