Converting Base 10 Numbers to Base 2 8 or 16

Introduction

This article explains the general meaning of number bases. Common base 10 numbers are used to lead into the generalized form of a number expressed in any base, b. Afterwards, the reader is given a walk-through conversion to base 8 (octal), followed by a brief introduction to base 2 (binary) and base 16 (hexadecimal)

Everyday base 10

We use the “base 10” number as our normal base system. In base 10 there are 10 unique numerals (0, 1, 2, 3, 4, 5, 6, 7, 8, and 9) used to represent all the natural numbers (0, 1, 2, 3, …). Of course, the decimal point “.” and minus sign “-” let us represent decimals and negative numbers, but these symbols are universal and apply across all base systems.

What does it mean to use the base 10 system? When one spends $35 at a store, we automatically understand that they spend 3*10 + 5 = 30 + 5 = 35 dollars. This is because base 10 is our normal representation of numbers in everyday life. In general, any base 10 number N can be represented as a weighted sum of powers of 10:

N = a_m*10^m + a_m1*10^(m-1) + … + a_2*10^2 + a_1*10 + a_0 [eq 1]

where all the a_m’s are just numerals (0,1,…,9),
and the m’s are any natural number (0,1,…,m).

Let’s look at an example.

Example #1: Base 10 sample

N = 9679

N = 9*10^3 + 6*10^2 + 7*10 + 9
= 9*(1000) + 6*(100) + 7*10 + 9
= 9000 + 600 + 70 + 9
= 9679

The powers of 10 can be thought of as a means of shifting the “weight” numeral to the correct position in the final number.

[End of example #1]

The general case

To generalize the representation to any base, b, just substitute “b” for “10” in [eq 1]:

N = a_m*b^m + a_m1*b^(m-1) + … + a_2*b^2 + a_1*b + a_0 [eq 2]

That is all there is to it! Eq 2 is the general expression of our base 10 number N in terms of some other base, b (b > 1).

Other bases

In case the end of the previous section has been a let down, let’s convert N from the first example into base 8. Numbers represented in base 8 are also known as “octal” numbers. Also, when writing numbers in different bases, it is common to subscript the base after the number, to avoid confusion. Here, I will just indicate the base in parenthesis.

Example #2: Convert N to base 8

N = 9679(base 10)
Express N in base 8.

The first thing we need to do is find the largest power of 8 that is less than or equal to N. Let’s list powers of 8 until we exceed N (9679):

8^0 = 1
8^1 = 8
8^2 = 64
8^3 = 512
8^4 = 4096
(8^5 = 32768 so we won’t need any powers of 8 above 8^4)

Find the maximum weight of 8^4 such that we don’t exceed N:
Procedurally, just divide N by 4096 to get 2.363037…, and keep the integer part.
So far, we know a_4 = 2.

Now we need to subtract this amount from N to see what’s left over to convert:
Remaining part to convert = N (a_4 * 8^4) = 9679 (2 * 4096) = 1487

Now 1487 / 512 = 2.90429… so a_3 = 2.
Remaining part to convert = 1487 (a_3 * 8^3) = 1487 (2 * 512) = 463

Now 463 / 64 = 7.2343… so a_2 = 7.
Remaining part to convert = 463 (a_2 * 64) = 463 (7 * 64) = 15

Now 15 / 8 = 1.875 so a_1 = 1.
Finally, a_0 = 15 (1 * 8) = 7, or a_0 = 7.

Answer: 9679(base 10) = 22717(base 8)

Notice how the base 8 answer uses only the numerals 0, 1, 2, 3, 4, 5, 6, and 7. We can check the value quite easily by eq 2:

Check: 2*8^4 + 2*8^3 + 7*8^2 + 1*8 + 7 =
2*4096 + 2*512 + 7*64 + 1*8 + 7 =
8192 + 1024 + 448 + 8 + 7 = 9679(base 10)

[End of example #2]

Base 2 and base 16

Base 2 and base 16 (as well as base 8) are very useful in computer hardware design and even in computer programming. Base 2 uses only the numerals 0 and 1. Base 16 uses the 16 “numerals” 0…9, plus a, b, c, d, e, and f for 10, 11, 12, 13, 14, and 15, respectively.

The original N = 9679(base 10) can be converted to base 2 and base 16 by following the procedure outlined in example #2. If you do that, you should get these results:

9679(base 10) = 10 0101 1100 1111 (base 2)
9679(base 10) = 25cf (base 16)

As a final point, you may have noticed the base 2 number was written in groups of 4 numerals, this is to highlight that there is a shortcut for converting between base 2 and base 16:

0000 (base 2) = 0 (base 16)
0001 = 1
0010 = 2
0011 = 3
0100 = 4
0101 = 5
0110 = 6
0111 = 7
1000 = 8
1001 = 9
1010 = a
1011 = b
1100 = c
1101 = d
1110 = e
1111 = f