2. Number Bases & Encodings
From hexadecimal to Base64 — representing values in any radix
In the previous article we saw that computers store numbers in base 2. But base 2 is just one choice among infinitely many. The very same quantity — say, the number forty-two — can be written as 101010 in binary, 52 in octal, 42 in decimal, or 2A in hexadecimal. None of these is more "true" than the others; they are just different notations for one value.
This article is about that freedom. First we'll generalize positional notation to any base and learn to convert between them. Then we'll meet a close cousin: binary-to-text encodings like Base64, which use the same base-conversion machinery to smuggle raw bytes through channels that only accept text.
Positional Notation, Generalized
A base (or radix) is just how many distinct digits you count with before you carry. In any base, a string of digits means:
Each position is worth times the one to its right. In base 10 the columns are 1, 10, 100, 1000. In base 2 they are 1, 2, 4, 8. In base 16 they are 1, 16, 256, 4096. Change and the whole scaffolding changes with it — but the rule never does.
Digits beyond 9
Base 10 has ten digits, 0–9. But what does a base-16 number do when it needs a digit worth ten? It borrows letters. Hexadecimal uses A=10, B=11, C=12, D=13, E=14, F=15. In general, a base up to 36 can use 0–9 followed by A–Z, giving 36 distinct digit symbols. The converter below goes all the way to base 36 on exactly this principle.
The Bases You Actually Meet
| Base | Name | Digits | Where you see it |
|---|---|---|---|
| 2 | Binary | 0 1 |
Raw machine state, bit masks |
| 8 | Octal | 0–7 |
Unix file permissions (chmod 755) |
| 10 | Decimal | 0–9 |
Everyday human numbers |
| 16 | Hexadecimal | 0–9 A–F |
Memory addresses, colors (#4ECDC4), byte dumps |
Hexadecimal earns its ubiquity because of a lucky coincidence: . That means each hex digit maps to exactly four bits — a nibble — with no arithmetic required. A byte (8 bits) is always exactly two hex digits. Octal has the same trick with three bits per digit, since , which is why old Unix permission bits come in threes.
11010110 splits cleanly into 1101 0110 = D6. This same "group the bits" idea powers Base64, later in this article.
Converting Between Bases
There are two directions, and each has a clean algorithm.
Any base → decimal uses the positional formula directly. Multiply each digit by its place value and sum. A tidy way to evaluate it is Horner's method: start at 0, and for each digit left to right, multiply the running total by the base and add the digit.
Converting hex 2A to decimal (Horner)
start: 0
digit '2': 0 × 16 + 2 = 2
digit 'A': 2 × 16 + 10 = 42
Decimal → any base uses repeated division, collecting remainders. Divide by the target base; the remainder is the next digit (least significant first); repeat with the quotient until it reaches zero.
Converting decimal 42 to base 16
42 ÷ 16 = 2 remainder 10 → digit 'A' (least significant)
2 ÷ 16 = 0 remainder 2 → digit '2' (most significant)
Reading remainders bottom-to-top: 2A
To convert between two non-decimal bases, the simplest route is to go through decimal (or, for a computer, through the raw integer value) as an intermediate. The interactive converter below does exactly that: it parses your input into a plain integer, then re-expresses that integer in the target base.
Try It — Base Converter
Enter a number, pick the base it's written in and the base to convert it to (anything from 2 to 36). The breakdown shows how each digit's place value adds up.
Result
Place-Value Breakdown
From Number Bases to Data Encodings
Everything so far treated the input as a number. But the same base machinery solves a completely different problem: how do you move arbitrary binary data — an image, a cryptographic key, a compressed blob — through a pipe that only allows text?
This comes up constantly. Email bodies (MIME), URLs, JSON strings, HTML data: URIs, and many config formats are all text-only. Raw bytes include control characters, quotes, and values that these formats would mangle or reject. The fix is a binary-to-text encoding: reinterpret the byte stream in a base whose digits are all safe, printable characters.
Base64
Base64 is the workhorse. Its "digits" are 64 characters — A–Z, a–z, 0–9, +, and / — chosen because they survive almost any text channel. Since , each Base64 character carries exactly 6 bits, and here the power-of-two trick returns: we just regroup the bit stream.
Bytes come in groups of 8 bits; Base64 symbols come in groups of 6. The least common multiple is 24 bits, so the natural unit is 3 bytes → 4 characters:
Padding. When the input length isn't a multiple of 3 bytes, the last group has spare bits. Base64 zero-fills the final symbol and appends = characters so the output length is always a multiple of 4. One = means the last group encoded 2 bytes; two = means it encoded 1 byte. That's how a decoder knows exactly how many real bytes to reconstruct.
Base32 and Base16
The same recipe works at other group sizes. Base16 is just hexadecimal applied to a byte stream — 4 bits per character, two characters per byte, no padding, and it doubles the size. Base32 uses 5-bit groups and a 32-character alphabet (A–Z and 2–7), working in units of 5 bytes → 8 characters. Base32 is handy when the text must survive case-insensitive systems, since its alphabet has no lowercase letters.
| Encoding | Bits/char | Alphabet size | Size overhead |
|---|---|---|---|
| Base16 (hex) | 4 | 16 | +100% |
| Base32 | 5 | 32 | +60% |
| Base64 | 6 | 64 | +33% |
Base64 wins on compactness, which is why it dominates email, data URIs, and JWT tokens.
When the base isn't a power of two
Not every encoding gets the clean bit-grouping shortcut. Base58 — used in Bitcoin addresses — drops the visually ambiguous characters (0, O, I, l) and the symbols +//. But 58 is not a power of two, so you can't just chunk bits. Encoding Base58 requires treating the entire byte blob as one gigantic integer and repeatedly dividing by 58 — the exact repeated-division algorithm from the first half of this article, just on very large numbers. The two halves of this article turn out to be the same idea.
Try It — Binary-to-Text Encoder
Type some text to encode, or paste an encoded string to decode. The visualization shows how the 8-bit bytes are regrouped into the encoding's output symbols.
Output
Bit Grouping
Key Takeaways
- A base (radix) is how many digits you count with before carrying. The positional value of a digit string is the sum of each digit times the base raised to its position.
- Any base up to 36 can be written with the digits 0–9 followed by A–Z. Hexadecimal (base 16) uses A–F for the values ten through fifteen.
- Convert to decimal with Horner's method (multiply-and-add); convert from decimal with repeated division, reading remainders bottom-to-top.
- When the base is a power of two, conversion to and from binary is just grouping bits — 4 bits per hex digit, 3 per octal digit — with no division.
- Binary-to-text encodings reuse this idea to push raw bytes through text-only channels. Base64 regroups 8-bit bytes into 6-bit symbols (3 bytes → 4 characters) and pads with
=. - Smaller alphabets (Base32, Base16) are more robust but bulkier; Base64's 33% overhead makes it the most common choice.
- Encodings whose base isn't a power of two — like Base58 — fall back to treating the data as one big integer and dividing repeatedly, exactly like ordinary base conversion.
You now have two complementary skills: reading a value in any base, and packaging raw bytes as safe text. Next we leave the integers behind and ask how to represent fractions in binary — starting with fixed-point numbers.