Feb-09-2019, 04:13 PM
You're on a viable track for the first problem. There are a few things to change.
First, getHexChar() can be greatly improved with a dict:
Now, decToHex() has a couple significant issues. The number 16 represented in hex is 10. If we run 16 through decToHex() we would have these results at the end of the first loop:
This indicates that 16 is evenly divisible by 16 exactly once; which is also what the hexadecimal 10 indicates as well. To accomplish the task then, it's evident that the quotient needs to be converted to hex. Also, the function must return the hexStr since it stores the end value:
As noted, the hex for 155 is 9B. Our result is 90 and the remainder that's ignored equals B, which is exactly what we're missing. Looking at the structure of the function again, we need a way to consider the remainder. So, let's add in some logic to check for value.
To fix this, we need to calculate the remainder based on the original decimal_number and not touch it again.
First, getHexChar() can be greatly improved with a dict:
def getHexChar(dec_digit): letters = { 10: "A", 11: "B", 12: "C", 13: "D", 14: "E", 15: "F" } return letters.get(dec_digit, dec_digit)The dict.get() method will check for a key matching dec_digit in letters and, if there is no match, it will return the default which is set to str(dec_digit).
Now, decToHex() has a couple significant issues. The number 16 represented in hex is 10. If we run 16 through decToHex() we would have these results at the end of the first loop:
Quote:16 // 16 == 1
16 % 16 == 0
This indicates that 16 is evenly divisible by 16 exactly once; which is also what the hexadecimal 10 indicates as well. To accomplish the task then, it's evident that the quotient needs to be converted to hex. Also, the function must return the hexStr since it stores the end value:
def decToHex(decimal_number): hexStr = '' while decimal_number != 0 : decimal_number = decimal_number // 16 remainder = decimal_number % 16 hexStr += getHexChar(decimal_number) return hexStrTesting this with 155 (hex 9B) returns 90. We're close but there's still more work to do. The problem is that now we are only examining decimal_number and ignoring the remainder. If we take a look at the math again, we see:
Quote:155 // 16 == 9
155 % == 11 <- 11 == 0xB
As noted, the hex for 155 is 9B. Our result is 90 and the remainder that's ignored equals B, which is exactly what we're missing. Looking at the structure of the function again, we need a way to consider the remainder. So, let's add in some logic to check for value.
def decToHex(decimal_number): hexStr = '' while decimal_number != 0 : decimal_number = decimal_number // 16 remainder = decimal_number % 16 if decimal_number == 0: hexStr += getHexChar(remainder) else: hexStr += getHexChar(decimal_number) return hexStrIt still gives us 90 instead of 9B for 155. The issue is with the remainder calculation. Because decimal_number gets reset before the modulus is calculated, it's doing this:
Quote:loop 1:
155 // 16 == 9
9 % 16 == 9
loop2:
9 // 16 == 0
0 % 16 == 0
To fix this, we need to calculate the remainder based on the original decimal_number and not touch it again.
def decToHex(decimal_number): hexStr = '' remainder = decimal_number % 16 while decimal_number != 0 : decimal_number //= 16 if decimal_number > 0: hexStr += getHexChar(decimal_number) else: hexStr += getHexChar(remainder) return hexStrThat gives us the correct value. Try it out for some other values. Now, here's a bigger problem. What's the hex of 15,232? The function only works on numbers up to 16^2 (256).