# Converting Between Binary, Decimal, and Hexadecimal

We are used to representing numbers using ten characters: $0,$ $1,$ $2,$ $3,$ $4,$ $5,$ $6,$ $7,$ $8,$ $9.$ This is called the **decimal** number system. However, there are other number systems that use more or fewer characters. For example, the **binary** or **base-$2$** number system uses two characters ($0$ and $1$), and the **hexadecimal** or **base-$16$** number system uses sixteen characters ($0,$ $1,$ $2,$ $3,$ $4,$ $5,$ $6,$ $7,$ $8,$ $9,$ $A,$ $B,$ $C,$ $D,$ $E$ where $A=11,$ $B=12,$ $C=13,$ $D=14,$ $E=15$).

Below is a table that illustrates how to count from zero to thirty-two in these different number systems. Each column corresponds to a different number system, and each row shows how the same quantity is represented in the three different number systems. The key pattern to notice is that you always increment the character in the rightmost digit, unless it has reached the last character, in which case you replace it with the first character and then increment the character of the adjacent digit on the left.

In general, if you have a number consisting of digits $x_n x_{n-1} \ldots x_1 x_0$ in base-$b,$ then you can convert it to decimal using the following formula:

For instance, the number $11010$ in binary (base-$2$) corresponds to the following number in decimal:

Likewise, the number $3\textrm A07\textrm E$ in hexadecimal (base-$16$) corresponds to the following number in decimal:

On the other hand, if you have a decimal number and you want to convert it to base-$b,$ then you can repeatedly compute the highest power of $b$ (call it $b^n$) that’s less than or equal to your decimal number, and then subtract off the largest multiple of $b^n$ that’s less than or equal to your decimal number.

For example, to convert the decimal number $26$ to binary (base-$2$):

- The largest power of $2$ that's less than or equal to $26$ is $2^4 = 16.$ The largest multiple of $16$ that's less than or equal to $26$ is $1 \cdot 16 = 16.$ Subtracting off, we have $26 - 16 = 10.$
- The largest power of $2$ that's less than or equal to $10$ is $2^3 = 8.$ The largest multiple of $8$ that's less than or equal to $10$ is $1 \cdot 8 = 8.$ Subtracting off, we have $10 - 8 = 2.$
- The largest power of $2$ that's less than or equal to $2$ is $2^1 = 2.$ The largest multiple of $2$ that's less than or equal to $2$ is $1 \cdot 2 = 2.$ Subtracting off, we have $2 - 2 = 0.$
- We subtracted off $1 \cdot 2^4,$ $1 \cdot 2^3,$ and $1 \cdot 2^1.$ Therefore, We can write $26$ as $1 \cdot 2^4$ $+ 1 \cdot 2^3$ $+ 0 \cdot 2^2$ $+ 1 \cdot 2^1$ $+ 0 \cdot 2^0,$ which has coefficients $1,$ $1,$ $0,$ $1,$ $0$ and therefore corresponds to the binary number $11010.$

Likewise, to convert the decimal number $241791$ to hexadecimal (base-$16$):

- The largest power of $16$ that's less than or equal to $241791$ is $16^4 = 65536.$ The largest multiple of $65536$ that's less than or equal to $241791$ is $3 \cdot 65536 = 196608.$ Subtracting off, we have $241791 - 196608 = 45183.$
- The largest power of $16$ that's less than or equal to $45183$ is $16^3 = 4096.$ The largest multiple of $8$ that's less than or equal to $45183$ is $11 \cdot 4096 = 8.$ Subtracting off, we have $45183 - 45056 = 127.$
- The largest power of $16$ that's less than or equal to $127$ is $16^1 = 16.$ The largest multiple of $16$ that's less than or equal to $127$ is $7 \cdot 16 = 112.$ Subtracting off, we have $127 - 112 = 15.$
- The largest power of $16$ that's less than or equal to $15$ is $16^0 = 1.$ The largest multiple of $1$ that's less than or equal to $15$ is $15 \cdot 1 = 15.$ Subtracting off, we have $15 - 15 = 0.$
- We subtracted off $3 \cdot 16^4,$ $11 \cdot 16^3,$ $7 \cdot 16^1,$ and $15 \cdot 16^0.$ Therefore, We can write $241791$ as $3 \cdot 16^4$ $+ 11 \cdot 16^3$ $+ 0 \cdot 16^2$ $+ 7 \cdot 16^1$ $+ 15 \cdot 16^0,$ which has coefficients $3,$ $11,$ $0,$ $7,$ $15$ and therefore corresponds to the hexadecimal number $3\textrm A07\textrm E.$

*Practice Problems*

Write the following functions. You can use string representations for all your inputs and outputs. As always, write a handful of tests for each function.

`binary_to_decimal(string)`

- takes a binary representation of a number and returns the decimal representation. For example,`binary_to_decimal('11010')`

should return`'26'`

.`hexadecimal_to_decimal(string)`

- takes a hexadecimal representation of a number and returns the decimal representation.`decimal_to_binary(string)`

- takes a decimal representation of a number and returns the binary representation.`decimal_to_hexadecimal(string)`

- takes a decimal representation of a number and returns the hexadecimal representation.`binary_to_hexadecimal(string)`

- takes a binary representation of a number and returns the hexadecimal representation. This is easy once you've written the functions above: just convert from binary to decimal, and then from decimal to hexadecimal.`hexadecimal_to_binary(string)`

- takes a binary representation of a number and returns the hexadecimal representation. This is easy once you've written the functions above: just convert from hexadecimal to decimal, and then from decimal to binary.

## Leave a Comment