Base conversions
The same number, written for three different readers — you (decimal), the machine (binary), and the datasheet (hex). This topic is how you move between them.
Built from first principles. Hex notation follows common datasheet convention (e.g. the MPU6050 register map).
Before you start
What you need first
- Number bases & place value — that each digit sits in a column worth a power of the base (binary: …8 4 2 1; hex: …256 16 1).
- The hex digits — 0–9 then A B C D E F stand for 10 11 12 13 14 15.
- Basic arithmetic — multiply, add, and divide-with-remainder.
What you'll be able to do
- Convert binary → decimal with the place-value method.
- Convert decimal → binary by repeated division by 2.
- Convert hex ↔ binary instantly, four bits at a time.
- Convert hex ↔ decimal, and know the smart route between them.
Motivation
One number, three sets of clothes
A microcontroller stores everything as binary — rows of on/off bits.
But raw binary is unreadable for a human: nobody can glance at
10110101 and know what it means.
So we wear different "clothes" for the same value. You think in decimal. A datasheet writes addresses and settings in hex because it is short. The chip works in binary. Converting between them is the everyday skill that lets all three talk.
10110101₂,
and 0xB5 are the same number.Binary → decimal: add the place values
Every bit sits in a column worth a power of two. To read a binary number in decimal, write each column's place value, keep only the columns where the bit is 1, and add them up.
Convert 10110101₂ to decimal
Lay the bits over their powers of two (rule of the house: show the power, not just the number):
| Position | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| Power | 2⁷ | 2⁶ | 2⁵ | 2⁴ | 2³ | 2² | 2¹ | 2⁰ |
| Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| Bit | 1 | 0 | 1 | 1 | 0 | 1 | 0 | 1 |
| Adds | 128 | · | 32 | 16 | · | 4 | · | 1 |
Add only the columns whose bit is 1:
✏️ Try it yourself
Convert 11010110₂ to decimal. (Tip: write the place values 128 64 32 16 8 4 2 1 above the bits.)
11010110₂ = 214.
Decimal → binary: divide by 2, read up
Go the other way by repeated division by 2. Each division leaves a remainder of 0 or 1 — those remainders are the bits. Read them bottom-to-top.
Convert 181 to binary
| Divide | = | Quotient | Remainder | |
|---|---|---|---|---|
| 181 ÷ 2 | = | 90 | 1 | ← LSB (read last) |
| 90 ÷ 2 | = | 45 | 0 | |
| 45 ÷ 2 | = | 22 | 1 | |
| 22 ÷ 2 | = | 11 | 0 | |
| 11 ÷ 2 | = | 5 | 1 | |
| 5 ÷ 2 | = | 2 | 1 | |
| 2 ÷ 2 | = | 1 | 0 | |
| 1 ÷ 2 | = | 0 | 1 | ← MSB (read first) |
Read the remainders from the bottom up:
10110101. Same answer.✏️ Try it yourself
Convert 200 to binary by repeated division by 2.
200 = 11001000₂. (Check: 128+64+8 = 200 ✓)
The easy one
Hex ↔ binary: four bits at a time
This is the conversion you will use most, and it needs no arithmetic at all. Because \(16 = 2^4\), one hex digit is exactly four binary bits (a "nibble"). Just group and substitute.
1011 0101 → 0xB5.Both directions
Binary → hex. Split into 4-bit groups from the right, then look each up:
| Binary | 1011 | 0101 |
| Hex | B | 5 |
Hex → binary. Replace each hex digit with its 4 bits:
| Hex | 2 | F |
| Binary | 0010 | 1111 |
0x2F as 0010 1111 after a few minutes of practice.✏️ Try it yourself
(a) Convert 0x3C to binary. (b) Convert 11100101₂ to hex.
0x3C = 00111100₂.
(b) group: 1110 0101 → E 5, so 11100101₂ = 0xE5.
Hex → decimal: powers of 16
Same place-value idea as binary, but the columns are now powers of 16: \(16^0=1,\ 16^1=16,\ 16^2=256,\ 16^3=4096,\dots\) Remember A–F mean 10–15.
Convert 0x2F to decimal
| Position | 1 | 0 |
|---|---|---|
| Power | 16¹ | 16⁰ |
| Place value | 16 | 1 |
| Hex digit | 2 | F = 15 |
Decimal → hex: divide by 16, read up
Exactly the repeated-division trick again — but divide by 16 instead of 2. If a remainder is 10–15, write it as A–F.
Convert 181 to hex
| Divide | = | Quotient | Remainder | |
|---|---|---|---|---|
| 181 ÷ 16 | = | 11 | 5 | ← read last |
| 11 ÷ 16 | = | 0 | 11 = B | ← read first |
Read the remainders bottom-to-top:
✏️ Try it yourself
(a) Convert 0x4A to decimal. (b) Convert 140 to hex.
0x4A = 74.
(b) 140 ÷ 16 = 8 r12 (=C); 8 ÷ 16 = 0 r8. Read up: 8 C. So 140 = 0x8C.
Check (b): \(8\times16 + 12 = 128 + 12 = 140\) ✓
In practice
Don't go decimal ↔ hex directly — go through binary
The divide-by-16 method works, but almost nobody uses it. The easy path is to route through binary, because hex ↔ binary is instant and you already know binary ↔ decimal.
Recap — the whole topic on one screen
| Conversion | Method | Worked result |
|---|---|---|
| Binary → decimal | Add the place values where the bit is 1 | 10110101₂ = 181 |
| Decimal → binary | Divide by 2 repeatedly, read remainders up | 181 = 10110101₂ |
| Hex → binary | Replace each hex digit with 4 bits | 0x2F = 00101111₂ |
| Binary → hex | Group bits in 4s from the right, look up | 10110101₂ = 0xB5 |
| Hex → decimal | Add digit × power of 16 | 0x2F = 47 |
| Decimal → hex | Divide by 16 repeatedly (or via binary) | 181 = 0xB5 |