# decimal integer to its corresponding hexadecimal value

Page 1 of 1

## 3 Replies - 11322 Views - Last Post: 01 October 2012 - 07:41 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=293596&amp;s=9fef103122770b326b8c7497eb290bf2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 q81101

Reputation: 0
• Posts: 28
• Joined: 28-September 12

# decimal integer to its corresponding hexadecimal value

Posted 29 September 2012 - 01:57 AM

When I enter a decimal integer the program should display the corresponding hexadecimal value.

And this is what I got so far.....

```decimal = eval(input("Enter an integer: "))

n = decimal

while n != 0:
if n % 16 == 10:
elif n % 16 == 11:
elif n % 16 == 12:
elif n % 16 == 13:
hexidecimal = 'D'
elif n % 16 == 14:
elif n % 16 == 15:
else:

print("The", str(n) + "its corresponding to", hexadecimal)
```

For example decimal of 15 will be F (hex)
29 will be 1D (hex)

Apparently, my code is not working.

can anyone help!

Is This A Good Question/Topic? 0

## Replies To: decimal integer to its corresponding hexadecimal value

### #2 midknight51

Reputation: 20
• Posts: 47
• Joined: 25-September 12

## Re: decimal integer to its corresponding hexadecimal value

Posted 29 September 2012 - 05:10 AM

Im no expert but I did notice you are in an infinite while loop. Since your code does nothing to modify n, n will always not equal 0. Also, I noticed your using the hex() function anyway so... why not just take the string the user inputs, convert it to an int and then use the hex() functions?

```decimal = input('Please type an integer: ')

print(decimal + ' is equivlent to ' + hexadecimal)
```

If im missing something please let me know. Im just not sure why you want to go through all that extra code when you use hex() anyway.

### #3 q81101

Reputation: 0
• Posts: 28
• Joined: 28-September 12

## Re: decimal integer to its corresponding hexadecimal value

Posted 29 September 2012 - 11:04 AM

midknight51, on 29 September 2012 - 05:10 AM, said:

Im no expert but I did notice you are in an infinite while loop. Since your code does nothing to modify n, n will always not equal 0. Also, I noticed your using the hex() function anyway so... why not just take the string the user inputs, convert it to an int and then use the hex() functions?

```decimal = input('Please type an integer: ')

print(decimal + ' is equivlent to ' + hexadecimal)
```

If im missing something please let me know. Im just not sure why you want to go through all that extra code when you use hex() anyway.

I have to use the loop, and I don't know how to use a loop to write a decimal to hexadecimal code.

### #4 midknight51

Reputation: 20
• Posts: 47
• Joined: 25-September 12

## Re: decimal integer to its corresponding hexadecimal value

Posted 01 October 2012 - 07:41 PM

Sorry for the delay, I figured your problem had been solved since you didnt answer for a while.
So if you HAVE to use a loop and your code needs to turn a decimal number into hex then...

```#Declaring all my global variables here
decimal = ''
playAgain = True #Assigned true at the begining so program will run.

#Beginning of Program
print('Welcome! Please enter a number you would like converted into hexadecimal: ')
decimal = input()

while playAgain = True:
hexadecimal = hex(int(decimal)) #Assign the variable hexadecimal the value of the conversion of the integer decimal.
print(decimal + ' is equivlent to ' + hexadecimal)
print('Would you like to try again? (y/n)')

#Now that the program is finished, lets see if the user is done.

if answer != 'y' or 'yes':
playAgain = False #If the user enters anything other than 'y' or 'yes', playAgain turns to False. Ending the while loop.
```

Im at work so I have no way to test this code. But that's the idea.
Hope this helps!