Mathematics RU

Solver

Number base converter with step-by-step working

Binary, octal, hexadecimal and any other base up to 36, fractions and Roman numerals, with step-by-step working and a check.

Loading the solver…

How to type a problem

The number, its base and what to convert it to: 1011_2 to 10, FF_16 to 2, 255 to 16. The base goes after the number: with an underscore (1010_2), in brackets (1010(2)) or as a subscript (1010₂); the programmer’s prefixes 0x, 0b, 0o are understood too. A number without a base is taken to be decimal. You can name the base in words: “to binary”, “to hexadecimal”, “to hex”. If you don’t say what to convert to, a decimal number is converted to binary, octal and hexadecimal all at once, and a non-decimal one to decimal.

Fractional numbers work too (0.1 to 2, 1A.8_16), including recurring ones (0.(3) to 3). Digits above nine are written as Latin letters: A = 10, B = 11, …, Z = 35, so the base can be anything from 2 to 36. Separately, the solver converts to Roman numerals and back: 2024 to Roman, MCMLXXXIV.

What a written number means

In $2024$ the two on the left means two thousand, while the two in the tens place means twenty: what a digit contributes depends on its place. This is called a positional notation, and the base is the factor by which neighbouring places differ. In the decimal system the places are units, tens, hundreds; in binary they’re units, twos, fours, eights.

The digits: whole numbers from $0$ to $b - 1$. There’s no digit “$b$” in base $b$: that many units of one place already make one unit of the next. The base of the system. The number of the place, counting from zero on the right. Digits after the point are in places numbered $-1, -2, \ldots$ and are worth $\frac{1}{b}, \frac{1}{b^2}, \ldots$ Example: $1011011_2 = 64 + 16 + 8 + 2 + 1 = 91$, and $\mathrm{FF}_{16} = 15 \cdot 16 + 15 = 255$.

This formula is itself the way to convert a number into decimal: write out what each digit contributes and add. For long numbers Horner’s scheme is handier: go through the digits from left to right, multiply what you have so far by the base and add the next digit. For $1011011_2$: $1 \to 2 \to 5 \to 11 \to 22 \to 45 \to 91$. No large powers need computing.

Out of decimal by division

Converting out of decimal is conveniently done the other way round, by division. Divide $n$ by the base $b$ with remainder: $n = b \cdot q + r$. By the formula above, every place except the last is divisible by $b$, so the remainder $r$ is the last digit, and the quotient $q$ holds all the other digits. Divide the quotient again to get the second-to-last digit, and so on down to zero. The remainders, read from bottom to top, are the number in the new base.

The fractional part is the mirror image. Multiply it by $b$: the whole part of the result is the first digit after the point, and the fractional part gets multiplied by $b$ again. If the fractional part becomes zero, the conversion is finished. If some fractional part repeats, the digits will go round in a cycle from there on: you’ve got a recurring fraction.

This leads to an unexpected consequence: $0.1$ in binary is the infinite fraction $0.0(0011)_2$. A fraction terminates in base $b$ only if the denominator of its lowest-terms form factorises into primes dividing the base, and $\frac{1}{10}$ has a five in the denominator. That’s why a computer stores $0.1$ approximately, and in most programming languages $0.1 + 0.2$ comes out slightly more than $0.3$.

Between 2, 8 and 16 by groups of digits

Eight is $2^3$ and sixteen is $2^4$. So one octal digit is exactly three binary places, and one hexadecimal digit exactly four. To convert a binary number to octal, split it into groups of three digits, starting from the point, and replace each group with a digit; for hexadecimal, groups of four. Going back, write each digit out as three or four bits. No division at all. That’s why programmers love hexadecimal: a byte is exactly two hexadecimal digits, from $00$ to $\mathrm{FF}$, and the colour #FF8800 reads as three bytes: red $255$, green $136$, blue $0$.

Between other bases, say from base five to base three, the shortest route is through decimal: first add up the contributions of the places, then divide.

Worked examples

Example 1. 255 to binary

Divide by $2$ and write down the remainders:

$$\begin{array}{r|r|c} n & n \div 2 & \text{remainder} \\ \hline 255 & 127 & 1 \\ 127 & 63 & 1 \\ 63 & 31 & 1 \\ 31 & 15 & 1 \\ 15 & 7 & 1 \\ 7 & 3 & 1 \\ 3 & 1 & 1 \\ 1 & 0 & 1 \end{array}$$

The remainders from bottom to top: $255 = 11111111_2$. It’s the largest number that fits in eight binary places: $2^8 - 1$. In hexadecimal the same number is $\mathrm{FF}$, in octal $377$.

Example 2. 123 to base five

$123 = 5 \cdot 24 + 3$, $24 = 5 \cdot 4 + 4$, $4 = 5 \cdot 0 + 4$. The remainders from bottom to top: $123 = 443_5$.

Check by converting back; it takes half a minute and catches almost every mistake: $4 \cdot 25 + 4 \cdot 5 + 3 = 100 + 20 + 3 = 123$. The solver does this check for every answer. Another quick estimate is the number of digits: $5^2 = 25 \le 123 < 125 = 5^3$, so the base-five form has exactly three digits.

Example 3. $1011011_2$ to hexadecimal

Split into fours from the right and pad the left group with a zero: $0101\ 1011$. The first group is $5$, the second is $8 + 2 + 1 = 11 = \mathrm{B}$. Answer: $1011011_2 = \mathrm{5B}_{16}$. Check: $5 \cdot 16 + 11 = 91$, and $1011011_2 = 91$.

Example 4. $10.625$ to binary

The whole part: $10 = 1010_2$. Multiply the fractional part by $2$ and take off the whole parts:

$$0.625 \cdot 2 = 1.25 \to 1,\qquad 0.25 \cdot 2 = 0.5 \to 0,\qquad 0.5 \cdot 2 = 1 \to 1.$$

The fractional part has run out, so the answer terminates: $10.625 = 1010.101_2$. That’s as it should be: $0.625 = \frac58$, and the denominator has only twos.

Example 5. 2024 in Roman numerals

Roman numerals aren’t positional, but they’re still written place by place: $2000 = \mathrm{MM}$, $20 = \mathrm{XX}$, $4 = \mathrm{IV}$. Together: $\mathrm{MMXXIV}$. A symbol is written at most three times in a row, so $4$, $9$, $40$, $90$, $400$ and $900$ are written by subtraction: the smaller symbol goes before the larger one. Only I, X and C can be subtracted, and only from the two nearest larger symbols: $99 = \mathrm{XCIX}$, not $\mathrm{IC}$. The largest number that can be written this way is $3999 = \mathrm{MMMCMXCIX}$.

Common mistakes

  • Reading the remainders from top to bottom. The first remainder is the last digit of the number. Read from bottom to top.
  • Forgetting zero places. $1011011_2$ has two zero digits; when converting to decimal they contribute nothing, but they hold their place in the number. Drop them and you get a different number.
  • Writing a digit the base doesn’t have. Octal has no digits $8$ and $9$; binary has nothing but $0$ and $1$. Writing $129_8$ is a mistake.
  • Mixing up letters and digits. $\mathrm{A}_{16}$ is $10$, not $1$; $\mathrm{1A}_{16} = 26$.
  • Not grouping bits from the point. Groups of three and four are counted from the point: to the left in the whole part, to the right in the fractional part, padding incomplete groups at the ends with zeros.
  • Dividing the fractional part instead of multiplying it. The whole part is converted by division, the fractional part by multiplication.

See also

The history of writing numbers, from notches on a bone to Leibniz’s binary system (1703) and computers, is in the chapter “Counting: from notches to bits”. Why a recurring decimal repeats and how to turn it into a fraction is shown by the fraction calculator. Remainders, which conversion rests on, are covered in detail by the congruence solver, and the prime factorisation solver will break a base or a denominator into primes.

Where this is explained

Other solvers