## 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 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}

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 7 ^{2} goes into 158 * 7^{2}) *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: 31x
_{7} - Proceed to Step I.

I'll complete the step here.

- 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}

That is our base-7 number: 158_{10} = 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

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 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: 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 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.

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

Hiyas,

Thanks for that link. Webpages that make use of latex math please me. :)

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.

429

_{10}= ???_{16}1. 429 / 16 = 26.8125; .8125*16 = 13; 13 in hex is D. So 0x??D

2. 26 / 16 = 1.625; 0.625 * 16 = 10; 10 in hex is A. So 0x?AD

3. 1 / 16 = 0.0625; 0.0625 * 16 = 1; 1 is 1 in hex, so 0x1AD.

429(dec) = 0x1AD(hex)

Hope that helped to clear it up. If you still have problems feel free to ask again.

Cheers and thanks for the comments!

Hey guys,

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.

Thanks for finding that!

158 base 10 should be equal to 314 in base 7.

I second/confirm/approve of or whatever this statement.