How to convert a number from base 4 to base 16?

3.1k Views Asked by At

Let us take the number $32$ in base $4$ for our example. In hexadecimal it is $E$, but according to my way it ends up being $32$, which is incorrect. I take the numbers in pairs of $2$, because $4^2=16$, however this doesn't seem to work the same way it does with $2^n$. The problem is $32$ is larger than $16$, but it is also given in base $4$, not in decimal, which further complicates this. I could convert the number to binary and then continue to hexadecimal from there, but I want to know the way to convert numbers from base $n$ to base $n^x$.

1

There are 1 best solutions below

0
On BEST ANSWER

A base $4$ representation $$(a_n \cdots a_0)_4$$ has value $$a_n \cdot 4^n + a_{n - 1} \cdot 4^{n - 1} + \cdots + a_1 \cdot 4^1 + a_0 \cdot 4^0 .$$ To write this in base $16$, we need to rewrite this quantity as a sum of powers of $16$. But powers of $16$ are themselves powers of $4$: $$\cdots + (a_3 \cdot 4 + a_2) \cdot 16^1 + (a_1 \cdot 4 + a_0) \cdot 16^0 .$$ So, the digits of the number in base $16$ are $$\ldots, \quad a_3 \cdot 4 + a_2, \quad a_1 \cdot 4 + a_0 .$$

Informally, this says that rewrite our number in base $16$, we can convert each $2$-digit string $a_{2 m + 1} a_{2m}$ separately. (If our base $4$ representation number has an odd number $2 m + 1$ of digits, we can simply append a $0$ in the $4^{2 m + 1}$ place, since this does not change the value of the representation.) In our case, we have

\begin{align*} 00_4 &= 0_{16} \\ 01_4 &= 1_{16} \\ 02_4 &= 2_{16} \\ 03_4 &= 3_{16} \\ 10_4 &= 4_{16} \\ &\,\,\vdots \\ 21_4 &= 9_{16} \\ 22_4 &= \mathrm{A}_{16} \\ &\,\,\vdots \\ 33_4 &= \mathrm{F}_{16} .\end{align*}

So, for example, $$\color{red}{10}\color{#00bf00}{03}\color{#3f3fff}{22}_4 = \color{red}{4}\color{#00bf00}{\mathrm{3}}\color{#3f3fff}{\mathrm{A}}_{16} .$$