Base conversion is the process of rewriting a number from one positional numeral system to another, such as turning a decimal number into binary or hexadecimal, without changing its value. Every base uses a fixed set of digits and gives each position a weight that is a power of the base. Converting between bases is a routine task in programming because computers store data in binary, humans read decimal, and hexadecimal offers a compact bridge between the two, appearing in memory addresses, color codes, and byte dumps.
Key takeaways
- A base (radix) sets how many digits a system uses and the weight of each position.
- Conversion changes representation only; the underlying value stays the same.
- Decimal is base 10, binary base 2, octal base 8, and hexadecimal base 16.
- Each hex digit equals exactly four binary digits, making the two easy to interconvert.
- Convert to decimal by summing digit times base-power; convert from decimal by repeated division.
- Prefixes like
0x,0b, and0osignal the base of a literal.
What is a positional number system?
In a positional system, a digit’s contribution depends on its position. In base 10 the number 345 means 3×100 + 4×10 + 5×1, where the position weights are powers of ten. The same idea generalizes: in base b, the rightmost digit has weight b^0, the next b^1, and so on.
The base also fixes the digit alphabet. Binary uses 0 and 1; decimal uses 0 through 9; hexadecimal needs sixteen symbols, so it uses 0–9 then A–F for the values ten through fifteen.
How do I convert from another base to decimal?
To read a number in base b as decimal, multiply each digit by its position weight and add the results. For the binary number 1011:
1×8 + 0×4 + 1×2 + 1×1 = 11
For the hex number 2A, where A is ten:
2×16 + 10×1 = 42
This expansion method works for any base. It is also the mental model behind the built-in parsers that most languages provide.
How do I convert from decimal to another base?
The standard method is repeated division. Divide the decimal number by the target base, record the remainder, and repeat with the quotient until it reaches zero. The remainders, read from last to first, form the converted number.
Converting 42 to binary:
- 42 ÷ 2 = 21 remainder
0 - 21 ÷ 2 = 10 remainder
1 - 10 ÷ 2 = 5 remainder
0 - 5 ÷ 2 = 2 remainder
1 - 2 ÷ 2 = 1 remainder
0 - 1 ÷ 2 = 0 remainder
1
Reading the remainders bottom to top gives 101010, which is indeed 42 in binary.
Why do binary and hexadecimal convert so easily?
Because 16 is 2^4, every hexadecimal digit corresponds to exactly four binary digits with no rounding or carrying across the boundary. You can convert by grouping bits in fours:
Binary: 1010 1111
Hex: A F -> AF
This exact mapping is why hex is the preferred human-readable form for binary data. A single byte is always two hex digits, so memory dumps, hashes, and color values stay aligned and compact. Octal has the same property with three bits per digit, since 8 is 2^3, which is why it appears in file permission notation.
How do I do base conversion in code?
Languages expose base conversion through parsing (string to integer) and formatting (integer to string). The integer in memory has no base of its own; the base is only a property of the text you read or write.
# Python
int("2A", 16) # 42 (parse hex)
int("101010", 2) # 42 (parse binary)
format(42, "b") # '101010' (to binary)
format(42, "x") # '2a' (to hex)
// JavaScript
parseInt("2A", 16); // 42
(42).toString(2); // '101010'
(42).toString(16); // '2a'
A common mistake is forgetting to pass the base to a parser, which then assumes decimal and either misreads or rejects the input.
What are the standard base prefixes?
| Base | Name | Digits | Common prefix |
|---|---|---|---|
| 2 | Binary | 0-1 | 0b |
| 8 | Octal | 0-7 | 0o or leading 0 |
| 10 | Decimal | 0-9 | none |
| 16 | Hexadecimal | 0-9, A-F | 0x |
These prefixes remove ambiguity. Without them, 10 could be two in binary, eight in octal, ten in decimal, or sixteen in hex. Watch out for the old convention where a leading zero means octal in some languages, which is a classic source of bugs when someone writes 010 expecting ten.
Where does base conversion show up in real code?
Base conversion is not just an exercise; it appears constantly in everyday programming, usually hidden inside a familiar task.
- Web colors. A CSS color like
#FF8800is three bytes in hex, one each for red, green, and blue. Reading or generating colors means converting between hex and decimal channel values. - File permissions. Unix permission modes such as
chmod 755are octal, where each digit encodes three permission bits, a direct use of base 8’s three-bits-per-digit property. - Bit flags and masks. Options are often packed into a single integer and read with bitwise operators, which is far easier to reason about when you view the value in binary or hex.
- Encoding and hashing. Checksums, hashes, and binary identifiers are almost always displayed in hex because it is compact and byte-aligned.
A practical tip when debugging bit-level logic: print the value in binary or hex rather than decimal. A permission or flag bug that looks like an arbitrary number in base 10 often becomes obvious once you see the individual bits.
To make the color case concrete, take the CSS value #FF8800. Splitting it into byte pairs gives three channels: FF, 88, and 00. Converting each hex pair to decimal by the expansion method yields the familiar channel values:
FF -> 15×16 + 15 = 255 (red)
88 -> 8×16 + 8 = 136 (green)
00 -> 0 = 0 (blue)
So #FF8800 is the same color as an rgb(255, 136, 0) notation. Going the other way, you convert each decimal channel back to a two-digit hex value and concatenate them, padding single digits with a leading zero so every channel stays two characters wide. This padding requirement is a frequent source of bugs: dropping the leading zero on a small value like 5 turns 05 into 5 and shifts every following digit, producing a completely wrong color.
How does base conversion handle fractions and negatives?
Fractional values convert in two parts: the integer part by repeated division, and the fractional part by repeated multiplication by the base, taking the integer results as the new digits. A key caveat is that a fraction that terminates in one base may repeat forever in another. One tenth is exact in decimal but repeats endlessly in binary, which is the root cause of floating-point rounding surprises like 0.1 + 0.2 not equaling exactly 0.3.
Negative integers are usually represented with a sign or, at the hardware level, with two’s complement rather than a simple sign digit, so converting a negative machine integer to binary shows the two’s complement bit pattern, not a minus sign.
Frequently asked questions
What are the most common bases in programming?
Base 2 (binary), base 8 (octal), base 10 (decimal), and base 16 (hexadecimal). Hex and binary dominate low-level work because they map cleanly onto how computers store data.
Why is hexadecimal used so often with binary?
Because 16 is a power of 2, each hex digit maps to exactly four binary digits. This makes hex a compact, exact shorthand for binary values with no rounding.
Does base conversion change the value of a number?
No. Conversion only changes the representation, not the quantity. The number forty-two is the same value whether written as 42, 0x2A, or 101010 in binary.
How do I convert a number in code?
Most languages parse a string in a given base and format an integer into another. In Python, int(‘2A’, 16) reads hex and format(n, ‘b’) writes binary.
What is a base prefix like 0x or 0b?
It is a marker that tells a reader or parser which base a literal uses: 0x for hexadecimal, 0b for binary, and often a leading 0 or 0o for octal.
How do I convert fractional numbers between bases?
Convert the integer part by repeated division and the fractional part by repeated multiplication by the base, but be aware many fractions do not terminate in binary.
Why does 0.1 not convert cleanly to binary?
One tenth has no finite representation in base 2, just as one third has none in base 10, which is the root cause of many floating-point rounding surprises.