See How Things Work

Converting between bases

Unit
Number Bases
Part
1. Number Bases
Lesson
1.3

Changing the base of a numeral changes how a number is written, never the number. This lesson converts a numeral of any base into base ten, by its expansion or by multiplying and adding from the left, and a base-ten number into any base by dividing again and again. It proves why the remainders are the digits, converts between two bases other than ten, and passes between binary, octal and hexadecimal by blocks of bits.

From base b to base ten

By lesson 1.1, every whole number has exactly one numeral in each base, and numerals of other bases are not new numbers, only new names for the old ones.⁠[1] To convert a numeral from one base to another is to find the numeral of the same number in the other base. The first method was used throughout lesson 1.1: write out the expansion and add.

Example (Converting by the expansion).

Convert 4013₅ to base ten.

  1. The places, from the right, are worth 1, 5, 25 and 125.
  2. 4013₅ = 4 × 125 + 0 × 25 + 1 × 5 + 3 × 1 = 500 + 0 + 5 + 3.

Answer: 4013₅ = 508.

A second method needs no powers at all. Read the digits from the left, and keep a running value: start with the first digit, and at each new digit multiply the running value by the base and add the digit.

digit 44digit 020× 5 + 0digit 1101× 5 + 1digit 3508× 5 + 34013₅ = 508
Figure. Converting 4013₅ by multiplying and adding from the left. The running value is 4, then 4 × 5 + 0 = 20, then 20 × 5 + 1 = 101, then 101 × 5 + 3 = 508. Each running value is the number named by the digits read so far: 4₅, 40₅, 401₅ and 4013₅.
Theorem (multiply and add).

Let a numeral in base b have the digits aₖ, …, a₁, a₀ from the left. Start with the value aₖ; then, for each following digit in turn, multiply the value by b and add that digit. The final value is the number named by the numeral.

Proof.

After the first digit the value is aₖ, the number named by the one-digit numeral aₖ. Suppose that after some digits the value is the number m named by the digits read so far. Writing the next digit d after them gives a numeral whose last digit is d and which without its last digit names m. By the Lemma of lesson 1.1, the number it names is m × b + d: the quotient by b is m and the remainder is d. That is exactly the next value. So after each digit the value is the number named by the digits read so far, and after the last digit it is the number named by the whole numeral.

The same procedure evaluates polynomials, where it is called Horner’s method;⁠[2] polynomials are studied in later units. It saves work: each digit costs one multiplication and one addition, and no power of the base is ever computed.

Example (A hexadecimal numeral, from the left).

Convert 2AF₁₆ to base ten by multiplying and adding.

  1. Start with 2.
  2. Next digit A = 10: 2 × 16 + 10 = 42.
  3. Next digit F = 15: 42 × 16 + 15 = 672 + 15 = 687.

Answer: 2AF₁₆ = 687, as lesson 1.1 found from the expansion.

Example (A binary numeral by doubling).

Convert 1100100₂ to base ten.

  1. With b = 2 each step doubles and adds the next bit: 1; 1 × 2 + 1 = 3; 3 × 2 + 0 = 6; 6 × 2 + 0 = 12.
  2. 12 × 2 + 1 = 25; 25 × 2 + 0 = 50; 50 × 2 + 0 = 100.

Answer: 1100100₂ = 100, in agreement with lesson 1.2, where 100 = 64 + 32 + 4.

From base ten to base b: repeated division

The other direction uses the division algorithm (Division lesson 1.5). Divide n by b to get a quotient and a remainder: the remainder is the rightmost digit of n in base b.⁠[3] Then divide the quotient by b, and so on, dividing each quotient by b; the remainders are the further digits, and the process ends when a quotient is 0. It gives the digits of n from right to left.⁠[4][5] The existence proof of lesson 1.1, which divides once more as long as the quotient allows,⁠[6] is this very procedure.

Theorem (repeated division).

Let b ≥ 2 and n ≥ 1. Divide n by b, then each quotient by b, until a quotient is 0. The remainders, read from the last division to the first, are the digits of n in base b, from the left. There are as many divisions as digits.

Proof.

By the Lemma of lesson 1.1, the remainder of n ÷ b is the last digit of the numeral of n, and the quotient is named by the numeral with that digit removed. So the second division works on the numeral with one digit fewer, and its remainder is the next digit from the right; and so on. Each division removes exactly one digit, so the quotient becomes 0 exactly when every digit has been removed: after as many divisions as the numeral has digits. The remainders appear from the rightmost digit to the leftmost, so they are read in the opposite order.

remainder100 = 33 × 3 + 1133 = 11 × 3 + 0011 = 3 × 3 + 223 = 1 × 3 + 001 = 0 × 3 + 11readup100 = 10201₃
Figure. Writing 100 in base three. Each line divides the quotient of the line above by 3. The remainders, read from the bottom up, are the digits from the left: 100 = 10201₃.
Example (100 in base three).

Convert 100 to base three.

  1. 100 = 33 × 3 + 1. Remainder 1: the last digit.
  2. 33 = 11 × 3 + 0. Remainder 0.
  3. 11 = 3 × 3 + 2. Remainder 2.
  4. 3 = 1 × 3 + 0. Remainder 0.
  5. 1 = 0 × 3 + 1. Remainder 1, and the quotient is 0, so the divisions end.

Answer: reading the remainders from the bottom up, 100 = 10201₃. Check by the expansion: 1 × 81 + 0 × 27 + 2 × 9 + 0 × 3 + 1 = 81 + 18 + 1 = 100. Five divisions gave five digits.

Example (A hexadecimal numeral, with letter digits).

Convert 2,026 to base sixteen.

  1. 2,026 = 126 × 16 + 10. The remainder 10 is the digit A.⁠[7]
  2. 126 = 7 × 16 + 14. The remainder 14 is the digit E.
  3. 7 = 0 × 16 + 7. The quotient is 0.

Answer: 2,026 = 7EA₁₆. Check: 7 × 256 + 14 × 16 + 10 = 1,792 + 224 + 10 = 2,026. A remainder from 10 to 15 is one digit, never two.

Example (A common mistake: reading the remainders downward).

Someone converts 13 to binary, gets the remainders 1, 0, 1, 1 in this order and writes 1011₂. Check it.

  1. The divisions: 13 = 6 × 2 + 1; 6 = 3 × 2 + 0; 3 = 1 × 2 + 1; 1 = 0 × 2 + 1.
  2. 1011₂ = 8 + 2 + 1 = 11, not 13: the remainders were read from the top down.
  3. The first remainder is the last digit. Read from the bottom up: 1101₂ = 8 + 4 + 1 = 13.

Answer: 13 = 1101₂. A quick check by the expansion catches a reversed numeral, unless its digits read the same both ways.

Example (The edge cases: n less than b, and n = 0).

Convert 7 to base eight and to base nine, and 0 to base two.

  1. 7 = 0 × 8 + 7: one division, quotient 0. So 7 = 7₈, a single digit. The same happens in base nine: 7 = 7₉.
  2. For 0 there is nothing to divide: the procedure makes no division at all, and 0 is written 0 in every base.
  3. 8 itself needs two divisions: 8 = 1 × 8 + 0 and 1 = 0 × 8 + 1, so 8 = 10₈.

Answer: 7₈, 7₉ and 0. A number less than the base is a single digit, the same in every base large enough to have that digit.

Caution.

Every division here is a division with remainder, and each one must satisfy both conditions of the division algorithm: the equation, and a remainder less than b. A remainder of b or more means that the quotient is too small, and it would put a number that is not a digit into the numeral. For large n, each division can be done by long division.

Lesson 1.2 found binary numerals another way, by taking the largest power of two first. The same works in any base: the leftmost digit is the quotient of n by the largest power of b not more than n, and the rest is done with the remainder. The reason: if the numeral of n is aₖ…a₁a₀, then n = aₖ × bᵏ + m, where m is the number named by aₖ₋₁…a₀. That row has k digits (zeros on its left change nothing), so m ≤ bᵏ − 1 (lesson 1.1, Exercise 5) and m < bᵏ. By the uniqueness in the division algorithm, aₖ is the quotient of n ÷ bᵏ and m the remainder; and bᵏ is the largest power of b not more than n, since n < bᵏ⁺¹ (lesson 1.1, Exercise 7).

Example (The largest place first).

Convert 2,026 to base sixteen by its places.

  1. The places are worth 1, 16, 256 and 4,096. The largest not more than 2,026 is 256 = 16².
  2. 2,026 = 7 × 256 + 234, since 7 × 256 = 1,792 and 8 × 256 = 2,048. The digit in place 2 is 7.
  3. 234 = 14 × 16 + 10. The digit in place 1 is 14 = E and the digit in place 0 is 10 = A.

Answer: 2,026 = 7EA₁₆, as before. This method finds the digits from the left, but it needs the powers of the base.

Game

Divide and read upward

Round
1
Score
0
Streak
0

Write 242 in base 6. Divide by 6, then divide each quotient by 6, until the quotient is 0. Then read the remainders.

  1. 242 =× 6 +

Between two bases other than ten

To convert from a base b to a base c, neither of them ten, go through base ten: find the number from its numeral in base b, then divide it repeatedly by c. Both steps use only base-ten arithmetic.

Example (From base three to base five).

Convert 212₃ to base five.

  1. 212₃ = 2 × 9 + 1 × 3 + 2 = 18 + 3 + 2 = 23.
  2. 23 = 4 × 5 + 3; 4 = 0 × 5 + 4. The remainders are 3 and 4.

Answer: 212₃ = 43₅. Check: 4 × 5 + 3 = 23.

Example (From base eight to base six).

Convert 47₈ to base six.

  1. 47₈ = 4 × 8 + 7 = 39.
  2. 39 = 6 × 6 + 3; 6 = 1 × 6 + 0; 1 = 0 × 6 + 1. The remainders are 3, 0, 1.

Answer: 47₈ = 103₆. Check: 1 × 36 + 0 × 6 + 3 = 39.

Binary, octal and hexadecimal

Octal and hexadecimal are easily converted to and from binary, and for that reason they are often used by computer programmers.⁠[8] The reason is that 8 = 2³ and 16 = 2⁴: each octal digit corresponds to a block of three binary digits, and each hexadecimal digit to a block of four.⁠[9]

base 10base 2base 8base 16
0000000
1000111
2001022
3001133
4010044
5010155
6011066
7011177
81000108
91001119
10101012A
11101113B
12110014C
13110115D
14111016E
15111117F
Table. The numbers 0 to 15 in base ten, in binary (written with four bits), in octal and in hexadecimal. The digits 0 to 7 of octal are the blocks of three bits 000 to 111, and the digits 0 to F of hexadecimal are the blocks of four bits 0000 to 1111.
Theorem (blocks of bits).

Cut the binary numeral of n into blocks of three bits, starting from the right, with zeros written on the left of the first block if it is short. Replace each block by the number it names, from 0 to 7. The result is the octal numeral of n. Blocks of four bits, each replaced by a digit from 0 to F, give the hexadecimal numeral.

Proof.

Take blocks of three; blocks of four work the same way with 16 = 2⁴. Block j, counted from 0 at the right, holds the bits of places 3j + 2, 3j + 1 and 3j, with digits c, d, e say. Their part of the expansion is c × 2³ʲ⁺² + d × 2³ʲ⁺¹ + e × 2³ʲ. Each power has the factor 2³ʲ, so by the rule “one more factor”, the associative property (Multiplication lesson 1.6) and the distributive property this part equals (c × 2² + d × 2 + e) × 2³ʲ. The number in parentheses is the number named by the block cde, which is at most 111₂ = 7 by lesson 1.2. And 2³ʲ is a product of 3j factors 2, which group into j groups of three; each group is 2³ = 8, so 2³ʲ = 8ʲ. So n is the sum of the block values times 8⁰, 8¹, 8², …: an expansion in base 8 whose digits are less than 8. Its leftmost digit is not 0, because the first block holds the leftmost 1 of the binary numeral. By the uniqueness of numerals in lesson 1.1, it is the octal numeral of n.

The zeros on the left of the first block change nothing, as zeros on the left never do; they are written only so that every block has full size.⁠[10] Going the other way, each octal digit is replaced by its three bits and each hexadecimal digit by its four bits, and zeros at the very left of the result are dropped. A byte, eight bits, is exactly two hexadecimal digits.⁠[11]

1011011102

Blocks of 3 bits (octal)

101
5
101
5
110
6

= 5568

Blocks of 4 bits (hexadecimal)

0001
1
0110
6
1110
E

= 16E16

Figure. The binary numeral 101101110₂ cut into blocks from the right: in threes it gives the octal numeral 556₈, in fours (with three zeros written on the left of the first block) the hexadecimal numeral 16E₁₆. All three name 366.
Example (Binary to octal and to hexadecimal).

Convert 101101110₂ to octal and to hexadecimal.

  1. Blocks of three from the right: 101 101 110. They name 5, 5 and 6. So the octal numeral is 556₈.
  2. Blocks of four from the right: 1 0110 1110; the first block is short, so write it 0001. They name 1, 6 and 14 = E. So the hexadecimal numeral is 16E₁₆.

Answer: 101101110₂ = 556₈ = 16E₁₆. Check, all in base ten: 256 + 64 + 32 + 8 + 4 + 2 = 366; 5 × 64 + 5 × 8 + 6 = 320 + 40 + 6 = 366; 1 × 256 + 6 × 16 + 14 = 256 + 96 + 14 = 366.

Example (Octal and hexadecimal to binary).

Convert 755₈ and 3F₁₆ to binary.

  1. 755₈: replace 7, 5, 5 by 111, 101, 101. So 755₈ = 111101101₂.
  2. 3F₁₆: replace 3 and F by 0011 and 1111, then drop the zeros at the left: 3F₁₆ = 111111₂.

Answer: 111101101₂ and 111111₂. Check: 755₈ = 448 + 40 + 5 = 493 and 256 + 128 + 64 + 32 + 8 + 4 + 1 = 493; 3F₁₆ = 48 + 15 = 63 = 2⁶ − 1.

Example (A common mistake: blocks from the left).

Someone converts 101101110₂ to hexadecimal by cutting from the left: 1011 0111 0, and writes B70₁₆. Check it.

  1. B70₁₆ = 11 × 256 + 7 × 16 + 0 = 2,816 + 112 = 2,928, not 366.
  2. Cutting from the left leaves a short block at the right end, and the bits of the ones place no longer stand in the last block.
  3. Blocks must start at the ones place: 1 0110 1110, which gives 16E₁₆.

Answer: 101101110₂ = 16E₁₆, not B70₁₆. The blocks are cut from the right, as the places of every base are counted.

Game

Blocks of bits

Round
1
Score
0
Streak
0

Which numeral of base 16 names the same number as 1001111112?

100111111

Exercises

Exercise .

Convert to base ten by multiplying and adding from the left: (a) 3201₄; (b) 1F4₁₆.

▸ Show solution

(a) 3201₄: 3; 3 × 4 + 2 = 14; 14 × 4 + 0 = 56; 56 × 4 + 1 = 225. Check: 3 × 64 + 2 × 16 + 0 × 4 + 1 = 192 + 32 + 1 = 225.

(b) 1F4₁₆: 1; 1 × 16 + 15 = 31; 31 × 16 + 4 = 496 + 4 = 500.

Exercise .

Convert 500 to base seven by repeated division, and check the result.

▸ Show solution

500 = 71 × 7 + 3; 71 = 10 × 7 + 1; 10 = 1 × 7 + 3; 1 = 0 × 7 + 1. Read from the bottom up: 500 = 1313₇. Check: 343 + 3 × 49 + 1 × 7 + 3 = 343 + 147 + 7 + 3 = 500.

Exercise .

Write 255 in bases two, eight and sixteen.

▸ Show solution

Base two: 255 = 127 × 2 + 1, 127 = 63 × 2 + 1, …, every remainder is 1 (eight times): 11111111₂. Base eight: blocks of three, 011 111 111, give 377₈. Base sixteen: blocks of four, 1111 1111, give FF₁₆.

Check: 3 × 64 + 7 × 8 + 7 = 192 + 56 + 7 = 255 and 15 × 16 + 15 = 255.

Exercise .

Convert 1221₃ to base four.

▸ Show solution

1221₃ = 27 + 2 × 9 + 2 × 3 + 1 = 27 + 18 + 6 + 1 = 52. Then 52 = 13 × 4 + 0; 13 = 3 × 4 + 1; 3 = 0 × 4 + 3. So 1221₃ = 310₄. Check: 3 × 16 + 1 × 4 + 0 = 52.

Exercise .

Convert (a) C3₁₆ to binary; (b) 11101011₂ to hexadecimal; (c) 1000000₂ to octal.

▸ Show solution

(a) C3₁₆: C = 1100 and 3 = 0011, so C3₁₆ = 11000011₂ (= 128 + 64 + 2 + 1 = 195 = 12 × 16 + 3).

(b) 11101011₂: blocks of four 1110 1011, that is 14 and 11: EB₁₆ (= 14 × 16 + 11 = 235).

(c) 1000000₂: blocks of three 001 000 000: 100₈ (= 64).

Exercise .

Explain why blocks of two bits give the digits of base four, and use it to convert 110110₂ to base four.

▸ Show solution

4 = 2², so the proof of the Theorem works with blocks of two bits: block j has the bits of places 2j + 1 and 2j, its part of the expansion is (c × 2 + d) × 2²ʲ = (c × 2 + d) × 4ʲ, and c × 2 + d is at most 3. So blocks of two bits give the digits of base four.

110110₂: blocks 11 01 10, that is 3, 1, 2: 312₄. Check: 3 × 16 + 1 × 4 + 2 = 54 = 32 + 16 + 4 + 2.

Exercise .

A number has three digits in base sixteen. How many digits can its binary numeral have? Give two reasons.

▸ Show solution

A number with three hexadecimal digits satisfies 16² ≤ n < 16³, that is 256 ≤ n < 4,096 = 2¹². Since 256 = 2⁸, its binary numeral has at least 9 and at most 12 bits (lesson 1.1, Exercise 7: n has k + 1 digits in base 2 exactly when 2ᵏ ≤ n < 2ᵏ⁺¹).

The same follows from blocks: three hexadecimal digits are twelve bits, and the first digit, which is not 0, has between one and four significant bits.

Summary

  1. A conversion finds the numeral of the same number in another base. The number itself does not change.
  2. To base ten: add up digit × place value, or start with the first digit and, for each next digit, multiply by the base and add the digit (Horner’s method).
  3. From base ten to base b: divide by b, then each quotient by b, until the quotient is 0. The remainders are the digits from right to left, so they are read from the last division to the first.
  4. Between two other bases, go through base ten.
  5. 8 = 2³ and 16 = 2⁴, so blocks of three bits are octal digits and blocks of four bits are hexadecimal digits; the blocks are cut from the right.

Sources

  1. 1.
    John H. Conway and Richard K. Guy, The Book of Numbers (Copernicus (Springer), 1996).
    Chapter 1, “The Romance of Numbers”, section “Numbers in Other Bases”, p. 21 (the whole topic: pp. 21).
    “binary numbers are not a new kind of number”
    Used for: numerals of another base are not new numbers but other names for the same numbers (the sentence goes on: “they’re just the old numbers under a different name”).
  2. 2.
    Kenneth H. Rosen, Discrete Mathematics and Its Applications (8th ed., McGraw-Hill, 2019).
    Chapter 3, “Algorithms”, section 3.3, “Complexity of Algorithms”, Exercise 14, p. 242 (the whole topic: pp. 242–243).
    “It is called Horner’s method.”
    Used for: the procedure of multiplying the running value by the base and adding the next coefficient is Horner’s method for evaluating polynomials (the exercise gives it on pp. 242–243).
  3. 3.
    Kenneth H. Rosen, Discrete Mathematics and Its Applications (8th ed., McGraw-Hill, 2019).
    Chapter 4, “Number Theory and Cryptography”, section 4.2, “Integer Representations and Algorithms” (4.2.2, “Representations of Integers”: “Base Conversion”), p. 261 (the whole topic: pp. 261–263).
    “The remainder, a₀, is the rightmost digit in the base b expansion of n.”
    Used for: dividing n by b, the remainder is the rightmost digit of n in base b.
  4. 4.
    Kenneth H. Rosen, Discrete Mathematics and Its Applications (8th ed., McGraw-Hill, 2019).
    Chapter 4, “Number Theory and Cryptography”, section 4.2, “Integer Representations and Algorithms” (4.2.2, “Representations of Integers”: “Base Conversion”), p. 261 (the whole topic: pp. 261–263).
    “Continue this process, successively dividing the quotients by b, obtaining additional base b digits as the remainders. This process terminates when we obtain a quotient equal to zero. It produces the base b digits of n from the right to the left.”
    Used for: dividing each quotient by b gives the further digits as remainders; the process ends at a quotient 0 and gives the digits from right to left.
  5. 5.
    Kenneth H. Rosen, Discrete Mathematics and Its Applications (8th ed., McGraw-Hill, 2019).
    Chapter 4, “Number Theory and Cryptography”, section 4.2, “Integer Representations and Algorithms” (4.2.2, “Representations of Integers”: “Base Conversion”), Algorithm 1, p. 263 (the whole topic: pp. 261–263).
    “In Algorithm 1, q represents the quotient obtained by successive divisions by b, starting with q = n. The digits in the base b expansion are the remainders of these divisions”
    Used for: the algorithm for base b: successive divisions by b starting from n, the digits being the remainders.
  6. 6.
    David M. Burton, Elementary Number Theory (7th ed., McGraw-Hill, 2011).
    Chapter 4, “The Theory of Congruences”, section 4.3, “Binary and Decimal Representations of Integers”, p. 69 (the whole topic: pp. 69–70).
    “we can divide once more”
    Used for: Burton’s existence proof divides the quotient by b once more, and again, as long as it allows (the sentence begins “If q₁ ≥ b”).
  7. 7.
    Kenneth H. Rosen, Discrete Mathematics and Its Applications (8th ed., McGraw-Hill, 2019).
    Chapter 4, “Number Theory and Cryptography”, section 4.2, “Integer Representations and Algorithms” (4.2.2, “Representations of Integers”: “Octal and Hexadecimal Expansions”), p. 261 (the whole topic: pp. 260–261).
    “Usually, the hexadecimal digits used are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F, where the letters A through F represent the digits corresponding to the numbers 10 through 15 (in decimal notation).”
    Used for: a remainder from 10 to 15 is written as one of the hexadecimal digits A to F.
  8. 8.
    John H. Conway and Richard K. Guy, The Book of Numbers (Copernicus (Springer), 1996).
    Chapter 1, “The Romance of Numbers”, section “Numbers in Other Bases”, p. 21 (the whole topic: pp. 21).
    “The octal (base 8) and hexadecimal (base 16) systems are easily convertible to the binary system and so are often used by computer programmers.”
    Used for: octal and hexadecimal are easily converted to and from binary, and so are often used by programmers.
  9. 9.
    Kenneth H. Rosen, Discrete Mathematics and Its Applications (8th ed., McGraw-Hill, 2019).
    Chapter 4, “Number Theory and Cryptography”, section 4.2, “Integer Representations and Algorithms” (4.2.2, “Representations of Integers”: “Conversion between Binary, Octal, and Hexadecimal Expansions”), p. 263 (the whole topic: pp. 263).
    “Conversion between binary and octal and between binary and hexadecimal expansions is extremely easy because each octal digit corresponds to a block of three binary digits and each hexadecimal digit corresponds to a block of four binary digits”
    Used for: each octal digit corresponds to a block of three bits and each hexadecimal digit to a block of four.
  10. 10.
    Kenneth H. Rosen, Discrete Mathematics and Its Applications (8th ed., McGraw-Hill, 2019).
    Chapter 4, “Number Theory and Cryptography”, section 4.2, “Integer Representations and Algorithms” (4.2.2, “Representations of Integers”: “Conversion between Binary, Octal, and Hexadecimal Expansions”), Example 7, p. 263 (the whole topic: pp. 263).
    “we group the binary digits into blocks of three, adding initial zeros at the start of the leftmost block if necessary.”
    Used for: the bits are grouped in blocks, with zeros added at the start of the leftmost block when needed.
  11. 11.
    Kenneth H. Rosen, Discrete Mathematics and Its Applications (8th ed., McGraw-Hill, 2019).
    Chapter 4, “Number Theory and Cryptography”, section 4.2, “Integer Representations and Algorithms” (4.2.2, “Representations of Integers”: “Octal and Hexadecimal Expansions”), p. 261 (the whole topic: pp. 261).
    “Bytes, which are bit strings of length eight, can be represented by two hexadecimal digits.”
    Used for: a byte, eight bits, is two hexadecimal digits.