Converting Between Binary, Decimal, and Hexadecimal

by Justin Skycak (@justinskycak) on

There are other number systems that use more or fewer than ten characters.

This post is part of the book Introduction to Algorithms and Machine Learning: from Sorting to Strategic Agents. Suggested citation: Skycak, J. (2021). Converting Between Binary, Decimal, and Hexadecimal. In Introduction to Algorithms and Machine Learning: from Sorting to Strategic Agents. https://justinmath.com/converting-between-binary-decimal-and-hexadecimal/


Want to get notified about new posts? Join the mailing list and follow on X/Twitter.

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,$ $F,$ where $A=10,$ $B=11,$ $C=12,$ $D=13,$ $E=14,$ $F=15$).

Counting Demonstration

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 that case, you replace it with the first character and then increment the digit directly to the left.

$\begin{align*} \begin{matrix} \textrm{Decimal} & \textrm{Binary} & \textrm{Hexadecimal} \\ \hline \\ 0 & 0 & 0 \\ 1 & 1 & 1 \\ 2 & 10 & 2 \\ 3 & 11 & 3 \\ 4 & 100 & 4 \\ 5 & 101 & 5 \\ 6 & 110 & 6 \\ 7 & 111 & 7 \\ 8 & 1000 & 8 \\ 9 & 1001 & 9 \\ 10 & 1010 & \textrm A \\ 11 & 1011 & \textrm B \\ 12 & 1100 & \textrm C \\ 13 & 1101 & \textrm D \\ 14 & 1110 & \textrm E \\ 15 & 1111 & \textrm F \\ 16 & 10000 & 10 \\ 17 & 10001 & 11 \\ 18 & 10010 & 12 \\ 19 & 10011 & 13 \\ 20 & 10100 & 14 \\ 21 & 10101 & 15 \\ 22 & 10110 & 16 \\ 23 & 10111 & 17 \\ 24 & 11000 & 18 \\ 25 & 11001 & 19 \\ 26 & 11010 & 1\textrm A \\ 27 & 11011 & 1\textrm B \\ 28 & 11100 & 1\textrm C \\ 29 & 11101 & 1\textrm D \\ 30 & 11110 & 1\textrm E \\ 31 & 11111 & 1\textrm F \\ 32 & 100000 & 20 \\ \end{matrix} \end{align*}$


Converting from Base-$b$ to Decimal

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:

$\begin{align*} x_n \cdot b^n + x_{n-1} \cdot b^{n-1} + \ldots + x_1 \cdot b^1 + x_0 \cdot b^0 \end{align*}$


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

$\begin{align*} &1 \cdot 2^4 + 1 \cdot 2^3 + 0 \cdot 2^2 + 1 \cdot 2^1 + 0 \cdot 2^0 \\ &= 26 \end{align*}$


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

$\begin{align*} &3 \cdot 16^4 + \textrm B \cdot 16^3 + 0 \cdot 16^2 + 7 \cdot 16^1 + \textrm F \cdot 16^0 \\ &3 \cdot 16^4 + 11 \cdot 16^3 + 0 \cdot 16^2 + 7 \cdot 16^1 + 15 \cdot 16^0 \\ &= 241791 \end{align*}$


Converting from Decimal to Base-$b$

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$), we do the following:

  • 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$), we do the following:

  • 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 $4096$ that's less than or equal to $45183$ is $11 \cdot 4096 = 45056.$ 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 B07\textrm F.$

Exercises

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.

  1. binary_to_decimal(string)
    Take a binary representation of a number and return the decimal representation. For example, binary_to_decimal('11010') should return '26'.

  2. hexadecimal_to_decimal(string)
    Take a hexadecimal representation of a number and return the decimal representation.

  3. decimal_to_binary(string)
    Take a decimal representation of a number and return the binary representation.

  4. decimal_to_hexadecimal(string)
    Take a decimal representation of a number and return the hexadecimal representation.

  5. binary_to_hexadecimal(string)
    Take a binary representation of a number and return 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.

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


This post is part of the book Introduction to Algorithms and Machine Learning: from Sorting to Strategic Agents. Suggested citation: Skycak, J. (2021). Converting Between Binary, Decimal, and Hexadecimal. In Introduction to Algorithms and Machine Learning: from Sorting to Strategic Agents. https://justinmath.com/converting-between-binary-decimal-and-hexadecimal/


Want to get notified about new posts? Join the mailing list and follow on X/Twitter.