Project Gimu

Software research and development

Number Conversion Tricks

Curious about how to convert numbers from hexadecimal to binary as quick as possible? I’ve got good news for you, the following post contains methods to help you accomplish exactly that!

1.0 Conversion of Akin Bases

Assume we have to convert from one base to another (a \rightarrow b).

1.1 Higher Base to Lower Base

If we have to convert two bases in the form of a = b^n, then simply convert each digit from base a to base b. Add leading zeroes to each converted digit until the length of n has been reached. The last step is to combine them.

6421_8 \rightarrow 110|100|010|001_2 \rightarrow 110100010001_2

Each digit gets converted to binary and the last two resulting binary numbers also get leading zeroes (as 8 = 2^n \rightarrow n = 3).

1.2 Higher Base to Lower Base

Furthermore, if we have to convert two bases in the form of a^n = b, then simply split the number in base a in intervals of n digits. Proceed to convert the resulting numbers from base a to base b.

231211_4 \rightarrow 23|12|11_4 \rightarrow B65_{16}

The former number gets split in two-digit intervals (as 4^n = 16 \rightarrow n = 2). The resulting numbers finally get converted from base a to base b.

2.0 Conversion of Different Bases

If both bases do not have any correlation as seen above, proceed to use the following algorithm:

  1. Divide the given number with the target base b (b has to be converted to base a first).
  2. Remember the remainder
  3. Repeat step 1 with the result of the previous division (not the remainder!) until it becomes 0

Combine the remainders from bottom to top to get the desired result.

2.1 Power Of Two

There’s another trick that we can apply to the algorithm from 2.0 when the base of b is a power of two:

Example: Convert the given number to octal (8 = 2^n \rightarrow n = 3).
Given number: 11100111_2, base: 8_8 \rightarrow 1000_2

The last n digits are the remainder, the first ones are essentially the division results (the sections are divided by |).

11100|111_2 / 1000_2 = 11100_2 Remainder: 111_2
(0)11|100 / 1000_2 = 11_2 Remainder: 100_2
(00)0|011 / 1000_2 = 0_2 Remainder: 011_2

Read from top to bottom instead of bottom to top!

111|100|011_2 \rightarrow 347_8

Leave a Reply