Step 5: Convert a Decimal Number Into Any Base

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.

  1. 73 = 343 which is larger than 158 so we can't divide a larger number into a smaller number. Step down an exponent.
  2. 72 = 49 which is smaller than 158. 49 goes into 158 three times with a remainder. 3 * 49 = 147.
  3. 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.

  1. 158 - 147 = 11
  2. Since there is one seven in 11, place that in the tens place of the new number: 31x7
  3. Proceed to Step I.

I'll complete the step here.

  1. How many 71's are in 11? One and a remainder. 1 * 7 = 7.
  2. 11 - 7 = 4
  3. There are no seven's in four: 4 x 1 = 4
  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.

  1. 28 is 256 and is too big. 27 is 128. 128 goes into 158 one time.
  2. 158 - 128 = 30
  3. 24 = 16. 30 - 16 = 14.
  4. 23 = 8. 14 - 8 = 6.
  5. 22 = 4. 6 - 4 = 2.
  6. 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.

<p>And here you can find the formal math to convert number in any base to any other base: http://www.codinghelmet.com/?path=exercises/converting-number-bases</p>
<p>Hiyas,</p><p>Thanks for that link. Webpages that make use of latex math please me. :)</p>
<p>Alright, my question is about decimal to hexadecimal. When you have the number (9 in your example) and divide it by 16 and it equals 0, it works fine. But when you have a number like 429dec, which provides 26, which divides by 16 to 1, what do you do. I tried multiplying the equation by 1 and moving it over, and the converter showed a different result.</p>
Hi Zack, Ok, if I understand what you're unsure about, let's take the number you gave and work it out using my method.<br> <br> 429<sub>10</sub> = ???<sub>16</sub>&nbsp;<br> <br> 1. 429 / 16 = 26.8125; .8125*16 = 13; 13 in hex is D. So 0x??D<br> 2. 26 / 16 = 1.625; 0.625 * 16 = 10; 10 in hex is A. So 0x?AD<br> 3. 1 / 16 = 0.0625; 0.0625 * 16 = 1; 1 is 1 in hex, so 0x1AD.<br> 429(dec) = 0x1AD(hex)<br> <br> Hope that helped to clear it up. If you still have problems feel free to ask again.<br> Cheers and thanks for the comments!
<p>Hey guys,</p><p>Thanks for the comments. I checked and it looks like a typo as I have it correctly typed in the preceding line. The typo below is now corrected.</p><p>Thanks for finding that!</p>
<p><strong></strong></p><p>158 base 10 should be equal to 314 in base 7.</p>
<p>I second/confirm/approve of or whatever this statement.</p>
<p>Good Job. Please confirm the conversion of <strong>158 (Base 10) into base7 the result it should give is 314 (Base 7), not 214 (Base 7). </strong></p><p><strong>Just cross-check if there were any errors during calculations.</strong></p><p><strong>Weldone !!!</strong></p>
<br> You left out octal (base 8), which is just like base 10. If you're missing two fingers.<br> <div class="media_embed"> <a href="http://video.google.com/videoplay?docid=-7841878207694220233#">http://video.google.com/videoplay?docid=-7841878207694220233#</a></div> <br>
Oops, where did I leave out octal? <br><br><br>Ah, Tom Lehrer....
oops. I guess not. It was just missing from titles...<br>
where did you hear that joke from?
I think I saw it first on a thinkgeek t-shirt.

About This Instructable


22 favorites


Bio: Gian is a computational biologist and is the Managing Director at Open Design Strategies, LLC. He holds a BA in Molecular/Cellular Biology and an ... More »
More by nevdull: Create A Custom Medieval-/Fantasy-Style Calligraphy Quill Practical DACs Using Enumerated Types as Bitflags
Add instructable to: