Number bases: converting between binary, decimal and hex
Computers count in binary, programmers debug in hexadecimal, and humans count in decimal, the same quantity wearing different masks. A base converter translates a number between any two bases from 2 to 36, showing how positional notation works: each digit is multiplied by the base raised to the power of its position.
TL;DR
- In any base, a number is a sequence of digits where each position carries a weight equal to the base raised to a power.
- Formula: Value = Σ digit[i] × base^(n-1-i)
- Example 1: Binary 1010 to decimal — Digits: 1, 0, 1, 0
How it works
In any base, a number is a sequence of digits where each position carries a weight equal to the base raised to a power. Converting between bases means evaluating the source representation into its numeric value and then re-expressing that value in the target base by repeated division.
The math
To convert from a source base, multiply each digit by the source base raised to its positional power (from right to left, starting at 0) and sum the products. To convert into a target base, repeatedly divide the value by the target base, collecting the remainders from last to first as the result digits.
Each digit at position i (from the left, n digits total) is weighted by the base raised to the positional exponent, and the weighted digits are summed.
Examples
Example 1: Binary 1010 to decimal
- Digits: 1, 0, 1, 0
- Weights: 2³, 2², 2¹, 2⁰ = 8, 4, 2, 1
- Value: 1×8 + 0×4 + 1×2 + 0×1 = 10
- Result: 1010₂ = 10₁₀
Example 2: Decimal 255 to hexadecimal
- Divide 255 by 16 → 15 remainder 15
- 15 is digit F
- Result digits: F, F
- Result: 255₁₀ = FF₁₆
When to use it
✦ Programming and Debugging
Developers translate between binary, hex, and decimal for color codes, memory addresses, masks, and byte values.
✦ Computer Science Education
Students practice the mechanics of positional notation across bases to understand how computers store and represent numbers.
⚠ Common mistakes
- Using Invalid Digits: Every digit must be smaller than the source base; a digit of 2 is invalid in binary input.
- Reversing the Remainders: Remainders of the division process must be read from last to first to form the target digits.
- Confusing Base Symbols: The letters A-F represent 10-15 in hexadecimal; beyond base 36, letters are exhausted.
FAQ (2)
What is the most common base for computers?
Binary (base 2) is the native computer base, with hexadecimal (base 16) used as a compact human-readable shorthand.
Why can bases go up to 36?
Because the 10 decimal digits plus the 26 letters of the alphabet provide 36 distinct symbols for digits.