Numeric Conversions in Computing

When working with computers, we typically use three [and to some degree, four] numbering systems. The most common to the reader will be Decimal, a numbering system with ten digits. Digital computers work with a second numbering system—Binary—with only two digits. The third most common numbering system is Hexadecimal, a sixteen digit system. Hexadecimal is often abbreviated as “hex,” which should not be confused with the six digit system, Heximal or Hexal. The last and least common numbering system is Octal, an eight digit numbering system.

Rules for numbering systems

All the numbering systems (not just computer-related) have two features in common. First, all start at ZERO, rather than one; for that reason, the highest value for a digit is ONE LESS than the total number of digits allowed.

Base or “Radix” Allowed Digits
Binary 0,1
Octal 0,1,2,3,4,5,6,7
Decimal 0,1,2,3,4,5,6,7,8,9
Hexadecimal 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F

Note that hexadecimal uses letters for the last six digits beyond decimal “9”. This is merely a convention that eliminates the need for “inventing” a new character to represent one of those digits.

The second common feature is the how individual digits are “lined up” to form a number in a given base. In a series of digits, each has a “place” which indicates relative magnitude in relation to the base number. In decimal, we’re taught that we have the “one’s place”, the “ten’s place”, the “hundred’s place”, etc., for as many places as we want to add (we won’t worry about fractional representations in this discussion) . In reality, however, we’re really indicating the magnitude of that place based on the powers of the base.

That is to say, the “one’s place” is based on “ten to the zero power”, or 10º = 1. The “ten’s place” is based on “ten to the first power”, or 10¹ = 10. The “hundred’s place” is based on “ten to the second power”, or 10² = 100.

We do likewise in all the numbering systems: If the base or radix is N, then the places (in increasing magnitude) are Nº, N¹, N², etc. The sum of the digits multiplied by the magnitude gives the “true value” of a number. If the bushel-basket contains seventeen apples, then that number of apples can be expressed in the following manner:

Base or “Radix” “seventeen”
Binary 10001b
Octal 21o
Decimal 17d
Hexadecimal 11h

Note the use of a lower-case letter to indicate which numbering system is in use for all the representations of “seventeen”. Where it may be ambiguous or my intention is specific, I’ll be using that convention to indicate the different numbering systems.

Converting between systems

Because we “live & breathe” the decimal system, our best method of converting an arbitrary number from one base to another requires two steps: convert the number to decimal, then convert decimal to the new base.

Convert to decimal

Converting to decimal should be simple: The digit in each place is converted to decimal (which is trivial in all but Hex) and multiplied by the “place” in decimal. Again, the place is numbered from right-to-left, starting at zero. Then the base is taken to the place number and multiplied by the digit.:

Step FE04h
1) separate all the places F E 0 4
2) convert each place to decimal 15 14 0 4
3) Assign a magnitude multiplier 16³ = 4096 16² = 256 16¹ = 16 16º = 1
4) multiply the magnitude times the place value 15 × 4096 = 61440 14 × 256 = 3584 0 × 16 = 0 4 × 1 = 4
5) add all the results together 61440 + 4584 + 0 + 4 = 65028

2705o = (2 × 8³) + (7 × 8²) + (0 × 8¹) + (5 × 8º) = 1477d

1101b = (1 × 2³) + (1 × 2²) + (0 × 2¹) + (1 × 2º) = 13d

Convert from decimal

Conversion from decimal is simply a sequence of divisions, and rules for using the results of the division. In any conversion, the decimal number is divided by the decimal value for the base: 2, 8, 16 (or any other base). The remainder of the division is then converted to the base, and starting from the Nº place, the new number is “built” to the Nª place after “a” steps. If the quotient of the division is greater than the base, then another division is performed, and the remainder is converted and added to the next place of the new number. This proceeds until the quotient is less than the base. At this point, the quotient is converted from decimal to the base, and is added to the next place in the new number.:

1447d in binary:

Step Number
1) 1477 ÷ 2 = 738 remainder 1 1
2) 738 ÷ 2 = 369 remainder 0 01
3)  369 ÷ 2 = 148 remainder 1 101
4) 184 ÷ 2 = 92 remainder 0 0101
5) 92 ÷ 2 = 46 remainder 0 00101
6) 46 ÷ 2 = 23 remainder 0 000101
7) 23 ÷ 2 = 11 remainder 1 1000101
8) 11 ÷ 2 = 5 remainder 1 11000101
9) 5 ÷ 2 = 2 remainder 1 111000101
10) 2 ÷ 2 = 1 remainder 0 0111000101
11) 1 < 2, so convert and place 10111000101

1447d in hexadecimal:

Step Number
1) 1477 ÷ 16 = 92 remainder 5 5
2) 92 ÷ 16 = 5 remainder 12 C5
3) 5 < 16, so convert and place 5C5

Neat tricks for Hex, Octal to Binary (and back!)

The conversion between hex or octal and binary is actually far easier than the steps I’ve shown above, which is actually why the two numbering systems became widely used in computing: octal and hex become “shorthand” for binary numbers.

As it turns out, the first three binary places (2², 2¹, 2º) can convert to exactly all the digits in the octal system, which represents the first place in that system. For each three additional places in the binary system, another one place in the octal system is generated. This gives programmers a three-to-one “compression” of the number of characters needed to represent a given binary number.

Hexadecimal takes this shortcut one place farther, by encompassing four binary places per hexadecimal place. In practice, that gives you a direct conversion between the two systems and binary.

From the above example, 2705o is converted to binary...

Step 2705o
1) separate all the places 2 7 0 5
2) convert each place to a three-place binary, adding leading zeros if needed 010 111 000 101
3) string the 3-place binaries together 010 111 000 101
4) remove or add leading zeroes (as applicable) 10111000101b

Another example: 10111000101b converted to hex...

Step 10111000101b
1) separate into blocks of four places, adding leading zeros if needed 0101 1100 0101
2) convert each block to a hex digit 5 C 5
3) string the hex digits together 5 C 5
4) remove or add leading zeroes (as applicable) 5C5h

The last “trick” is to use binary as the “pivot” between octal and hex (instead of decimal), by combining the above two examples:

Step 2705o
1) separate all the places 2 7 0 5
2) convert each place to a three-place binary, adding leading zeros if needed 010 111 000 101
3) string the 3-place binaries together 010111000101
4) separate into blocks of four places, adding leading zeros if needed 0101 1100 0101
5) convert each block to a hex digit 5 C 5
6) string the hex digits together 5 C 5
7) remove or add leading zeroes (as applicable) 5C5h

Comments? Email jim3@millard.org Last updated October 20, 2001