1.5 Math for a Digital Age
1.5.9 Converting to any base

Most people already know how to do many number conversions. For instance, when converting inches to yards, first divide the number of inches by 12 to get the number of feet. The remainder is the number of inches left. Next divide the number of feet by 3 to get the number of yards. The remainder is the number of feet left. These same techniques are used for converting numbers to other bases.

If converting from decimal (the normal base) to octal, Base 8 (known here as the foreign base) for example, divide by 8 (the foreign base) successively and keep track of the remainders starting from the least significant remainder.

Take the number 1234 in decimal and convert it to octal.

1234 / 8 = 154 R 2
154 / 8 = 19 R 2
19 / 8 = 2 R 3
2 / 8 = 0 R 2

The result is 2322 in octal.

To convert back again, multiply a running total by 8 and add each digit successively starting with the most significant number.

2 * 8 = 16
16 + 3 = 19
19 * 8 = 152
152 + 2 = 154
154 * 8 = 1232
1232 + 2 = 1234

An easier way of achieving the same results in the above reverse conversions is by using numerical powers:

2*83 + 3*82 + 2*81 + 2*80 = 1024 + 192 + 16 + 2 = 1234.

Note that any number raised to the power of zero is one.

Similar techniques can be used to convert to and from any base just by dividing or multiplying by the foreign base.

However, binary is unique in that odd and even can be used to determine ones and zeros without recording the remainders. Given the same number that we used above, 1234, determine the binary equivalent simply by dividing it by 2 successively. The bit is determined by the odd and evenness of the result. If the result is even, the bit associated with it is 0. If the result is odd, the binary digit associated with it is 1.

1234 is even -- record a 0 in the least significant position.

1234/2 = 617 is odd -- record a 1 in the next most significant position (10)

617/2 = 308 is even (010)
308/2 = 154 is even (0010)
154/2 = 77 is odd (10010)
77/2 = 38 is even (010010)
38/2 = 19 is odd (1010010)
19/2 = 9 is odd (11010010)
9/2 = 4 is even (011010010)
4/2 = 2 is even (0011010010)
2/2 = 1 is odd (10011010010)

With practice, the running dividend can be mastered and the binary can be written quickly.

Note that just as a hexadecimal digit is a group of four bits, octal is a group of three digits. Group the above number into groups of three starting at the right:

010,011,010,010 = 2322 octal

For hex, group by four bits:

0100,1101,0010 = 4D2 hexadecimal or 0x4D2

This is a quick and easy method to convert to any base.