Need help with dec to hex program

Page 1 of 1

5 Replies - 1009 Views - Last Post: 21 June 2010 - 09:34 AMRate 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=178455&amp;s=c9d33a3c82f6b76b4d93acf7fd997600&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 LemurGuy

Reputation: 0
• Posts: 18
• Joined: 27-September 09

Need help with dec to hex program

Posted 20 June 2010 - 12:47 PM

I am asked to do a dec to hex converter without using the built in hex() function, so I made the following code:

```input_num = input("What is the integer you'd like to convert to hexadecimal? ")

def dec_hex_conv(input_num):
while_num = input_num
for_num = input_num
hex_string = ""
hex_digit_count = 1
while while_num / 16 > 0:
while_num = while_num / 16
hex_digit_count = hex_digit_count + 1

for i in range(0, hex_digit_count):

power = hex_digit_count - 1
divisor = 16 ** power
power = power - 1

hex_num = for_num / divisor
for_num = for_num % divisor

if hex_num >= 0 and hex_num <= 9:
hex_string = hex_string + str(hex_num)
elif hex_num == 10:
hex_string = hex_string + "A"
elif hex_num == 11:
hex_string = hex_string + "B"
elif hex_num == 12:
hex_string = hex_string + "C"
elif hex_num == 13:
hex_string = hex_string + "D"
elif hex_num == 14:
hex_string = hex_string + "E"
else:
hex_string = hex_string + "F"

return hex_string
print dec_hex_conv(input_num)

```

For values of 1 through 15, I get the correct answers
For 16, I correctly get 10
For 17, I get 10 (incorrect, should be 11)
When I input 255, I get F0 (incorrect again)
With 4095, I get F00 (which should be FFF) etc.
So somehow I've managed to screw up the code in a way that I only get zeros in the last digit(s) when I input a number larger than 16.

Trouble is, I'm very new to Python programming and can't quite trace where the program is going wrong.

Any advice, hints, or tiny code snippets to help me learn would be much appreciated!

Is This A Good Question/Topic? 0

Replies To: Need help with dec to hex program

#2 LemurGuy

Reputation: 0
• Posts: 18
• Joined: 27-September 09

Re: Need help with dec to hex program

Posted 20 June 2010 - 03:31 PM

Just a heads up for anyone looking to help:

The line containing "power = hex_digit_count - 1" just needed to be moved out of the for loop. I moved it just above the beginning of the for loop, with the same indentation, and it works fine now.

If mods want to close or delete the thread, that's fine with me.

All good now.

#3 Motoma

Reputation: 450
• Posts: 795
• Joined: 08-June 10

Re: Need help with dec to hex program

Posted 20 June 2010 - 03:49 PM

You were so close!!

You need to move the following line to before the for loop:
```power = hex_digit_count - 1

```

Otherwise, you reinitialize the power variable each iteration of the for loop.

LemurGuy, on 20 June 2010 - 04:31 PM, said:

Just a heads up for anyone looking to help:

The line containing "power = hex_digit_count - 1" just needed to be moved out of the for loop. I moved it just above the beginning of the for loop, with the same indentation, and it works fine now.

If mods want to close or delete the thread, that's fine with me.

All good now.

Beat me to it!

#4 atraub

• Pythoneer

Reputation: 731
• Posts: 1,877
• Joined: 23-December 08

Re: Need help with dec to hex program

Posted 20 June 2010 - 06:03 PM

Just a suggestion, you could replace all this with one line of code if you implemented a dictionary. Research it

```if hex_num >= 0 and hex_num <= 9:
hex_string = hex_string + str(hex_num)
elif hex_num == 10:
hex_string = hex_string + "A"
elif hex_num == 11:
hex_string = hex_string + "B"
elif hex_num == 12:
hex_string = hex_string + "C"
elif hex_num == 13:
hex_string = hex_string + "D"
elif hex_num == 14:
hex_string = hex_string + "E"
else:
hex_string = hex_string + "F"

```

#5 Motoma

Reputation: 450
• Posts: 795
• Joined: 08-June 10

Re: Need help with dec to hex program

Posted 21 June 2010 - 04:27 AM

atraub, on 20 June 2010 - 07:03 PM, said:

Just a suggestion, you could replace all this with one line of code if you implemented a dictionary. Research it

```if hex_num >= 0 and hex_num <= 9:
hex_string = hex_string + str(hex_num)
elif hex_num == 10:
hex_string = hex_string + "A"
elif hex_num == 11:
hex_string = hex_string + "B"
elif hex_num == 12:
hex_string = hex_string + "C"
elif hex_num == 13:
hex_string = hex_string + "D"
elif hex_num == 14:
hex_string = hex_string + "E"
else:
hex_string = hex_string + "F"

```

Even a dictionary is not needed; a simple string will do:
```hex_chars = '0123456789ABCDEF'
...
hex_string += hexchars[hex_num]

```

#6 atraub

• Pythoneer

Reputation: 731
• Posts: 1,877
• Joined: 23-December 08

Re: Need help with dec to hex program

Posted 21 June 2010 - 09:34 AM

That is the exact same concept as a dictionary but admittedly yours does have less overhead.

However, I was trying to encourage the users to learn a powerful data structure for themselves rather than give them the answer. Hence why I said:

Quote

Research it

This post has been edited by atraub: 21 June 2010 - 09:36 AM