Convert a denery number into any base-n
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 15810 into base7
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.
- 73 = 343 which is larger than 158 so we can't divide a larger number into a smaller number. Step down an exponent.
- 72 = 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: 3xx7
II. Subtract the product of the multiplication from the dividend of the division. This sounds overly complicated. Put another way, 158 - 147 = 11. We're subtracting (the number of times 72 goes into 158 * 72) from the original base-10 number, or our starting number.
- 158 - 147 = 11
- Since there is one seven in 11, place that in the tens place of the new number: 31x7
- Proceed to Step I.
I'll complete the step here.
- How many 71'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: 3147
That is our base-7 number: 15810 = 3147
Convert denery into binary
What is 15810 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.
- 28 is 256 and is too big. 27 is 128. 128 goes into 158 one time.
- 158 - 128 = 30
- 24 = 16. 30 - 16 = 14.
- 23 = 8. 14 - 8 = 6.
- 22 = 4. 6 - 4 = 2.
- 21 = 2. 2 - 2 = 0
Notice I didn't move any numbers into the target base locations this time. In binary, we'll either have it going into a number one or zero times. Look back at the calculations as I add the numbers.
128 + 16 + 8 + 4 + 2
128 64 32 16 8 4 2 1 1 0 0 1 1 1 1 0
So the decimal number 15810 is 100111102.
Convert decimal into hexadecimal
Ready to try your hand at converting decimal to hexadecimal? Good, I thought so.
What is 15810 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: 0xXE
Take 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 15810 is 9E16, 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 15810 into base-8.