CloudAcademia
EN

Networking · Module 03 · Binary and Hexadecimal Numbers

Lab: Converting Numbers by Hand

Lab30 minFoundationsMarkdown

Showing raw Markdown: the renderer could not load.

## Goal
Practise converting numbers between decimal, binary and hexadecimal with a place-value table, then read a real IP address and a MAC address byte by byte. At the end you can optionally let your computer check your answers.

## Objectives
- Convert numbers between decimal and binary (0 to 255).
- Convert numbers between binary and hexadecimal, and recognize hexadecimal in a MAC address.
- Read an IPv4 address as four 8-bit numbers.

## Prerequisites
- Paper and a pencil (or a text file).
- [The Binary and Hexadecimal Numbers lesson](lesson.md) of this module.
- Optional, for Step 4 only: Python 3 on your own computer. Nothing else is needed and you never need a cloud account.

> [!NOTE]
> Everything here is safe: you only do sums on paper. The addresses in this lab are for examples only: `192.168.1.10` is a private address (RFC 1918) and `00:00:5e:00:53:01` is a MAC address reserved for documentation (RFC 7042).

## Architecture
<figure class="diagram" style="--dg-w: 1040px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 1040 190" width="1040" height="190" role="img" aria-labelledby="binhex-lab-architecture-title binhex-lab-architecture-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="binhex-lab-architecture-title">Lab flow: four short activities, from paper to an optional Python check</title>
<desc id="binhex-lab-architecture-desc">Four numbered steps joined by arrows. Step 1, place-value table: fill in the eight columns for a few numbers. Step 2, an IP address: turn 192.168.1.10 into four bytes of bits. Step 3, hex and a MAC address: convert hex digits, then read 00:00:5e:00:53:01 byte by byte. Step 4, check with Python: optional, let the computer confirm your answers.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="1039" height="189" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<rect class="dg-card" x="24" y="28" width="222.5" height="134" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><circle class="dg-solid" cx="46" cy="52" r="12" fill="#0369a1"/><text x="46" y="56.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">1</text></g>
<g class="dg-l1"><g class="dg-icon" transform="translate(206.5 40) scale(1.0833)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="7" height="7" x="3" y="3" rx="1"/><rect width="7" height="7" x="14" y="3" rx="1"/><rect width="7" height="7" x="14" y="14" rx="1"/><rect width="7" height="7" x="3" y="14" rx="1"/></g></g>
<text x="38" y="90" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Place-value table</text>
<text x="38" y="110" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">Fill in the 8 columns</text>
<text x="38" y="127" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">for a few numbers</text>
<path class="dg-line" d="M252.5 95H274.5" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M266.5 89l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="280.5" y="28" width="222.5" height="134" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><circle class="dg-solid" cx="302.5" cy="52" r="12" fill="#0369a1"/><text x="302.5" y="56.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">2</text></g>
<g class="dg-l1"><g class="dg-icon" transform="translate(463 40) scale(1.0833)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><path d="M12 2a14.5 14.5 0 0 0 0 20 14.5 14.5 0 0 0 0-20"/><path d="M2 12h20"/></g></g>
<text x="294.5" y="90" font-size="14" class="dg-text" fill="#17171c" font-weight="700">An IP address</text>
<text x="294.5" y="110" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">Turn 192.168.1.10</text>
<text x="294.5" y="127" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">into 4 bytes of bits</text>
<path class="dg-line" d="M509 95H531" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M523 89l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="537" y="28" width="222.5" height="134" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><circle class="dg-solid" cx="559" cy="52" r="12" fill="#0369a1"/><text x="559" y="56.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">3</text></g>
<g class="dg-l1"><g class="dg-icon" transform="translate(719.5 40) scale(1.0833)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M12 20h.01"/><path d="M2 8.82a15 15 0 0 1 20 0"/><path d="M5 12.86a10 10 0 0 1 14 0"/><path d="M8.5 16.43a5 5 0 0 1 7 0"/></g></g>
<text x="551" y="90" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Hex and a MAC address</text>
<text x="551" y="110" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">Convert hex digits, then read</text>
<text x="551" y="127" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">00:00:5e:00:53:01 byte by byte</text>
<path class="dg-line" d="M765.5 95H787.5" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M779.5 89l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="793.5" y="28" width="222.5" height="134" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><circle class="dg-solid" cx="815.5" cy="52" r="12" fill="#0369a1"/><text x="815.5" y="56.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">4</text></g>
<g class="dg-l1"><g class="dg-icon" transform="translate(976 40) scale(1.0833)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 16V7a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v9m16 0H4m16 0 1.28 2.55a1 1 0 0 1-.9 1.45H3.62a1 1 0 0 1-.9-1.45L4 16"/></g></g>
<text x="807.5" y="90" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Check with Python</text>
<text x="807.5" y="110" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">Optional: let the computer</text>
<text x="807.5" y="127" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">confirm your answers</text>
</svg></figure>

*Figure 1. Steps 1 to 3 are done on paper; step 4 is an optional check on the computer.*

## Steps

### Step 1 — Fill in a place-value table (10 min)
Draw this row on your paper. It is your tool for everything else, so copy it exactly:

| Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|

**Part A: decimal to binary.** For each number, go from left to right. If the place value fits into what is left, write `1` and subtract it; if not, write `0`. Convert these five numbers to 8 bits:

1. 5
2. 13
3. 37
4. 100
5. 150

**Part B: binary to decimal.** Write each number under the place-value row and add the columns that hold a 1:

1. `00000110`
2. `01000001`
3. `10100000`
4. `11111110`

Try them all before opening the answers.

<details>
<summary>Show the answers</summary>

| Part A: decimal | Columns that fit | Binary |
|---|---|---|
| 5 | 4 + 1 | `00000101` |
| 13 | 8 + 4 + 1 | `00001101` |
| 37 | 32 + 4 + 1 | `00100101` |
| 100 | 64 + 32 + 4 | `01100100` |
| 150 | 128 + 16 + 4 + 2 | `10010110` |

| Part B: binary | Columns with a 1 | Decimal |
|---|---|---|
| `00000110` | 4 + 2 | 6 |
| `01000001` | 64 + 1 | 65 |
| `10100000` | 128 + 32 | 160 |
| `11111110` | 128 + 64 + 32 + 16 + 8 + 4 + 2 (everything except the 1) | 254 |

Shortcut for the last one: `11111111` is 255, and this number is missing the final 1, so it is 254.

If a result was wrong, add the columns of your binary answer back up. It should give the number you started with.

</details>

### Step 2 — Turn an IP address into bits (7 min)
Your home router often has an address like `192.168.1.10`. It is four bytes in decimal. Convert each byte to 8 bits with your table:

1. 192
2. 168
3. 1
4. 10

Then write the whole address as four groups of 8 bits separated by dots.

<details>
<summary>Show the answer</summary>

| Decimal | Columns that fit | Binary |
|---|---|---|
| 192 | 128 + 64 | `11000000` |
| 168 | 128 + 32 + 8 | `10101000` |
| 1 | 1 | `00000001` |
| 10 | 8 + 2 | `00001010` |

The address in binary is `11000000.10101000.00000001.00001010`. That is 4 × 8 = 32 bits in total.

</details>

### Step 3 — Hexadecimal and a MAC address (8 min)
**Part A: hex conversions.** Use the table `0=0000, 1=0001 … 7=0111, 8=1000, 9=1001, A=1010, B=1011, C=1100, D=1101, E=1110, F=1111` (you can rebuild it with the place values 8, 4, 2, 1).

1. Turn the hex byte `A7` into 8 bits, then into decimal.
2. Turn the hex byte `3F` into 8 bits, then into decimal.
3. Turn the binary byte `11010110` into two hex digits.

**Part B: read a MAC address.** Here is a MAC address reserved for documentation: `00:00:5e:00:53:01`.

1. How many bytes does it have, and how many hex digits?
2. Write each byte in decimal (hint: a hex byte is the first digit × 16 plus the second digit).
3. Write the byte `5e` in binary.

<details>
<summary>Show the answers</summary>

**Part A**

| Hex | Digit by digit | Binary | Decimal |
|---|---|---|---|
| `A7` | A = 10 = `1010`, 7 = `0111` | `10100111` | 167 (128 + 32 + 4 + 2 + 1) |
| `3F` | 3 = `0011`, F = 15 = `1111` | `00111111` | 63 (32 + 16 + 8 + 4 + 2 + 1) |
| `11010110` | `1101` = 13 = D, `0110` = 6 = 6 | | `D6` |

**Part B**

1. Six bytes, and 12 hex digits (two per byte).
2. `00` = 0, `00` = 0, `5e` = 5 × 16 + 14 = 94, `00` = 0, `53` = 5 × 16 + 3 = 83, `01` = 1. So the MAC address is 0, 0, 94, 0, 83, 1 in decimal.
3. `5e`: 5 = `0101`, e = 14 = `1110`, so `01011110`.

</details>

### Step 4 — Check your answers with Python (optional, 5 min)
You can skip this step. If you want the computer to check your work, you can use **Python**, a programming language that works like a very clever calculator. You don't need to know how to program: you type a short line and read the answer.

**Open a terminal** (a window where you type text commands):
- **Windows:** press the Start key, type `cmd`, press Enter. Then type `py` and press Enter.
- **macOS:** press Cmd + Space, type `Terminal`, press Enter. Then type `python3` and press Enter.
- **Linux:** open your terminal application. Then type `python3` and press Enter.

You should see a line starting with `>>>`. That is Python waiting for you. (If your computer says the command isn't found, Python isn't installed: skip this step, it is optional.) Type each line below and press Enter. Your Python version number may differ; the answers will not.

```python
bin(37)
int("00100101", 2)
format(37, "08b")
hex(200)
int("c8", 16)
format(200, "02x")
```

Output:
```text
'0b100101'
37
'00100101'
'0xc8'
200
'c8'
```

What each line does:
- `bin(37)` turns decimal 37 into binary. Python adds the prefix `0b` to show it is binary and drops the leading zeros, so `0b100101` is the same as `00100101`.
- `int("00100101", 2)` turns the binary text into a decimal number. The `2` means "this text is in base 2" (binary).
- `format(37, "08b")` turns 37 into binary again, but padded to 8 digits (`08b` means "8 characters, with zeros on the left, in binary"). This is the form you wrote by hand.
- `hex(200)` turns decimal 200 into hex. Python adds the prefix `0x`.
- `int("c8", 16)` turns hex text into a decimal number. The `16` means "this text is in base 16".
- `format(200, "02x")` turns 200 into hex padded to 2 digits (`02x` means "2 characters, zeros on the left, hex").

Now check one of your own answers. For example, the MAC byte `5e` and the IP byte 168:

```python
int("5e", 16)
format(168, "08b")
```

Output:
```text
94
'10101000'
```

Both match what you found on paper. Try a few more of your own numbers. To leave Python, type `exit()` and press Enter.

> [!NOTE]
> The quotes (`'...'`) in the output only show that Python is giving you text. They are not part of the number.

## Verification
You are done when:
- All your Part A and Part B answers in Step 1 match, or you found and understood each mistake.
- You wrote `192.168.1.10` as `11000000.10101000.00000001.00001010`.
- You can say why a MAC address has 6 bytes and 12 hex digits.
- Optional: Python gave the same answers as your paper.

## Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Your binary answer adds up to a different number | You skipped a column or subtracted wrongly | Redo it column by column, writing "left after" each time; check by adding the columns back up |
| Your binary number has fewer than 8 digits | You dropped the leading zeros | Pad on the left with zeros until you have 8 digits |
| A hex conversion looks wrong | You treated A to F as 1 to 6 | A is 10, B is 11, C is 12, D is 13, E is 14 and F is 15 |
| Python says `command not found` or `'py' is not recognized` | Python isn't installed on your computer | Skip Step 4; it is optional |
| Python shows `SyntaxError` | A quote or bracket is missing, or you typed `>>>` | Type the line exactly as shown, without the `>>>` |

## Challenge (optional)
Convert your own computer's or phone's IP address to binary. You can find it in your network settings (look for "IPv4 address"). It is your own value, so it will be different from the examples; most home addresses start with `192.168.` or `10.`. Then write the first byte in hex.

## Cleanup
Nothing to clean up: you only used paper and, optionally, a Python window. Type `exit()` to close Python if it is still open.

## What you learned
- A place-value table turns any number from 0 to 255 into 8 bits and back.
- One hex digit is 4 bits, so one byte is two hex digits.
- An IPv4 address is four bytes (32 bits), and a MAC address is six bytes (12 hex digits).
- A computer can check your work, but you can do all of this with a pencil.