Understanding the number base converter — concept and principle
A number base (radix) is the number of distinct digits a positional system uses. We count in base 10, computers work in base 2 (binary), and programmers often write binary values compactly in base 16 (hexadecimal) or base 8 (octal). This converter changes a whole number from any base between 2 and 36 to any other, and always shows the decimal, binary and hex forms as well.
How it is calculated
Reading the input. Each character is a digit: 0–9 have values 0–9 and the letters A–Z have values 10–35, upper or lower case. Every digit must be smaller than the "from" base. The value is built from left to right:
value = value × base + digit
which is the same as Σ dᵢ × baseⁱ, where i counts positions from the right starting at 0.
Writing the output. The value is divided repeatedly by the target base; the remainders, read in reverse order, are the new digits.
A leading minus sign is kept. Big integers are used, so long inputs stay exact.
The principle
In a positional system each position is worth the base times the position to its right: in base 10 the places are 1, 10, 100…, in base 2 they are 1, 2, 4, 8…, and in base 16 they are 1, 16, 256…. The same quantity can be written in any base; only the place values change. Dividing by the base and taking the remainder peels off the lowest digit, which is why repeated division produces the digits from right to left. Because 16 = 2⁴ and 8 = 2³, one hex digit is exactly 4 bits and one octal digit is 3 bits.
Worked example
- Inputs: number FF, from base 16, to base 2.
- F = 15, so FF = 15 × 16 + 15 = 255. Decimal: 255.
- Divide 255 by 2 repeatedly: 127 r 1, 63 r 1, 31 r 1, 15 r 1, 7 r 1, 3 r 1, 1 r 1, 0 r 1.
- Reading the remainders gives 11111111.
- Shortcut: each F is 1111 in binary, so FF = 1111 1111. Hex: FF.
How to read the result
In binary, n digits can represent values up to 2ⁿ − 1, so 255 is the largest value in one byte (8 bits). Two hex digits always describe one byte, which is why colours (#FF8800) and memory addresses are written in hex. If you see "invalid digits for this base", a digit is too large, such as 8 in an octal number or G in hex.
Common mistakes
- Reading the remainders top to bottom instead of bottom to top.
- Expecting fractional parts (such as 0.1) to convert; this tool converts whole numbers only.
- Including prefixes such as 0x or 0b in the input; enter only the digits.
Frequently asked questions
Why does base 36 stop at Z?
Ten numerals plus 26 Latin letters make 36 symbols, the most available without inventing new characters.
How do I convert binary to hex quickly?
Group the bits in fours from the right and replace each group with one hex digit: 1010 1111 becomes AF.
What is octal still used for?
Mainly for Unix file permissions, such as 755, where each octal digit encodes three permission bits.
Sources: D. E. Knuth, The Art of Computer Programming, Vol. 2: Seminumerical Algorithms, Section 4.1 (positional number systems) and Section 4.4 (radix conversion)