课程: Python Essential Training

Solution: Converting hex to decimal - Python教程

课程: Python Essential Training

Solution: Converting hex to decimal

- [Instructor] All right. I hope the hex didn't vex you too much. I have two solutions that I want to go over with you, and the first is pretty straightforward. So here I'm assuming that everything passed into this function is a string, or rather a list of characters is another way to think about it. And the only test cases I gave you were strings. So if you did check for integers or some other really wacky input, you are a more thorough and diligent programmer than I am. Good work, but I was really only worried about strings here. Just as an upfront check, I go through every character in hexNum and just make sure that it's in our dictionary up here. And if it is in our dictionary, then I know I can process it. So there are three cases, three character strings, two character strings, and one character strings. For the three character strings, I take the leftmost character, so that's the character index zero, and multiply it by 256 or 16 squared. Remember, this is the 256th place in a hexadecimal number. Then I take the middle number, multiply it by 16 because that's in the 16ths place, or 16 to the first power. Then I have the one's place and I just add that onto the rest and return it. I have a similar solution for two character strings and then one character strings. Okay, so I want to look at another approach that can handle actually any length of string, and that is down here. So this kind of a solution was definitely not required, but I think it's worth exploring. So here we have the exact same checking code that we had in the previous solution. If every character is in our hex numbers dictionary, we know we can process it. Then I added a placeholder called converted. It starts out as zero, and this is the number that we ultimately return. So we're going to loop through our string and sort of add all the numbers together. Then we want to figure out what the exponent for the largest place is in our hexadecimal number. Remember, in a hexadecimal number, you have the one's place, the 16ths place, the 256th place, the 4096th place, et cetera. So that's 16 to the zeroth power, 16 to the first power, 16 to the second, 16th to the third, et cetera. So in general, the highest power or the highest exponent we're looking at is the number of characters in the hexadecimal number minus one. So if we have a three character hex number, value of the leftmost character is 16 to the three minus one's place, or 16 squared, or 256. So if you're still confused, run through it a few times and you'll see what I mean. So here in our code, all I do is take the length of hex number minus one, and then that gives us the highest exponent, the highest exponent of 16 in our number. Then we're going to make our loop. For char in hexNum, we add the the new number to our converted number. So on the first pass, converted is going to be zero. We're not adding anything, but we take that hex number in the leftmost place, multiply it by 16 to the exponent power. Then in every loop, we subtract one from the exponent. So as we go from left to right, our exponents get smaller and smaller and smaller until it eventually becomes zero, and we are multiplying the hex number times one. Finally, we return the final converted number. If you ever run into a programming problem where you're not sure how to do it at first, try narrowing the scope. How do you do it for one character, for two characters, for three characters? Try to find a pattern, get a feel for it, and then approach the general case for all numbers of characters.

内容