Data Representation & Number Systems

base conversion

A single quantity can be written in many number systems — decimal, binary, hex, octal — and base conversion is the recipe for translating between them without changing the actual amount. Think of describing the same distance as 'a mile', 'about 1.6 kilometres', or '5280 feet': the distance is fixed; only the units and notation change. Converting bases is the same idea applied to the symbols a number is written in.

Going from any base to decimal is direct: multiply each digit by its place value and add. So 0b1011 is 1 times 8 + 0 times 4 + 1 times 2 + 1 times 1 = 11. Going from decimal to another base uses repeated division: divide by the target base, write down the remainder, repeat on the quotient, and the remainders read bottom-to-top give the answer. To turn 13 into binary: 13 / 2 = 6 remainder 1, 6 / 2 = 3 remainder 0, 3 / 2 = 1 remainder 1, 1 / 2 = 0 remainder 1, so reading the remainders from last to first gives 0b1101. Between binary and hex or octal there is a faster shortcut: just group bits (4 at a time for hex, 3 at a time for octal) because those bases are powers of two.

Base conversion matters whenever humans and machines have to meet in the middle: you type a decimal number, the machine stores binary, and a debugger shows you hex. The key honesty point is that converting bases never changes the value, only the way it is spelled — but it can change how easy the value is to read, which is the whole reason hex and octal exist. Fractions are the tricky case: a number that is exact in one base can become an endless repeating expansion in another, which is exactly why 0.1 in decimal has no exact binary form.

Convert decimal 156 to binary by repeated division by 2: remainders (bottom-up) give 0b10011100. Group those bits as 1001 1100 to read hex 0x9C directly. All three — 156, 0b10011100, 0x9C — name one number.

Decimal to binary by repeated division, then binary to hex by grouping four bits.

Conversion preserves value, never alters it. But fractions can betray you: a clean decimal like 0.1 becomes a non-terminating binary fraction, the root cause of floating-point rounding surprises.

Also called
radix conversion基底轉換換底