Step 5: Convert a Decimal Number Into Any Base
To me, this is the hardest part of converting between bases: from decimal to a different base. However, don't let my math conundrums confound you. The process is very simple and without pitfalls, if not a bit circuitous. We'll try one to get a feel for the process because the process is the same for every base, which is a good thing we don't have to remember a bunch of conversion rules. To prove this point subtly, I'll start off converting a denery number into an odd base, base-7.
Convert 158_{10 }into base_{7}
I. The first step is to take the target base to varying powers and attempt to divide it into the source number to get the largest part.
- 7^{3} = 343 which is larger than 158 so we can't divide a larger number into a smaller number. Step down an exponent.
- 7^{2} = 49 which is smaller than 158. 49 goes into 158 three times with a remainder. 3 * 49 = 147.
- Take the number of times 49 goes into 147 and use it as the first digit of the new base: 3xx_{7}
- 158 - 147 = 11
- Since there is one seven in 11, place that in the tens place of the new number: 31x_{7}
- Proceed to Step I.
- How many 7^{1}'s are in 11? One and a remainder. 1 * 7 = 7.
- 11 - 7 = 4
- There are no seven's in four: 4 x 1 = 4
- Place the four in the digits section of the base-7 number: 314_{7}
Convert denery into binary
What is 158_{10} in base-2? We can figure that out now that we have our rules in place. I'll go through it short form, but not an entirely terse fashion. You will need, however, to recall binary value placement. I'll add up the digits into the binary value at the end.
- 2^{8 }is 256 and is too big. 2^{7} is 128. 128 goes into 158 one time.
- 158 - 128 = 30
- 2^{4} = 16. 30 - 16 = 14.
- 2^{3} = 8. 14 - 8 = 6.
- 2^{2} = 4. 6 - 4 = 2.
- 2^{1} = 2. 2 - 2 = 0
128 + 16 + 8 + 4 + 2
128 64 32 16 8 4 2 1 1 0 0 1 1 1 1 0So the decimal number 158_{10} is 10011110_{2}.
Convert decimal into hexadecimal
Ready to try your hand at converting decimal to hexadecimal? Good, I thought so.
What is 158_{10} in base-16? First, let's use some intuition. How many digits should the hexadecimal number be? Two, right? The number 158 is less than FF, which is 255.
For converting from decimal to hexadecimal there is a different way that I learned and prefer and I am going to show it to you now. It is much easier -- in my opinion -- that what you've just done thus far.
The general heuristic is divide by 16, multiple remainder by 16, convert to hex.
158 / 16 = 9.875
Take the remainder (.875) and multiply it by 16 and round up if necessary..875 * 16 = 14.
This is the digits place and 14 in hexadecimal is E: 0xXETake the number without the remainder from the previous step, which is 9 and divide by 16, but since 9/16 = 0, move the nine over: 0x9E
The number 158_{10} is 9E_{16}, 0x9E or 9Eh hexadecimal. Isn't that much easier?
Convert denery into octal
Because we've covered that odd base-7 earlier, and it's just a bit of the same thing with using eights instead of sevens I'll leave this section as an exercise for the reader. Convert 158_{10 }into base-8.