# Hexadecimal Made Simple: Read and Convert Hex Like an IT Pro

In [the last post](https://adam2root.hashnode.dev/binary-for-beginners) you learned binary: how computers store everything as 0s and 1s. Binary is perfect for computers and hard work for people. Read `11000000101010000000000100001010` out loud and you'll lose your place halfway. So IT uses a shorthand called **hexadecimal**, or **hex**. You've already seen it without knowing: the MAC address `3C-52-82-1A-7F-09` from [Post 6](https://adam2root.hashnode.dev/what-is-a-network) is hex. So are Windows error codes like `0x80070005`, website colors like `#39FF14`, and the scary-looking columns in hacking tools. This post shows you how to read it, convert it, and recognize it.

## Counting in base 16

Decimal has ten digits (0 to 9). Binary has two (0 and 1). Hexadecimal, or **base 16**, needs sixteen digits. After 9 it runs out of number symbols, so it borrows letters:

`0 1 2 3 4 5 6 7 8 9 A B C D E F`

A means 10, B means 11, and so on up to F, which means 15. After F comes `10`, which in hex means sixteen.

![The numbers 0 to 15 in hex, binary and decimal: 0 to 9 stay the same, A is 10, B is 11, up to F which is 15 or 1111 in binary](https://cdn.hashnode.com/uploads/gql/6afe08476920da9a67541ede/3a41c082-ac1c-40ec-8b8f-b27750bb8027.png align="center")

Letters can be upper or lower case. `ff` and `FF` are the same number.

## Why hex? One hex digit = 4 bits

Here's the reason hex is everywhere: four bits can count from 0 (`0000`) to 15 (`1111`), which is exactly the range of one hex digit. So:

- **1 hex digit = 4 bits** (half a byte, sometimes called a **nibble**)
- **2 hex digits = 8 bits = 1 byte**

That makes converting between binary and hex easy. Split the byte in half and translate each half on its own.

![200 in decimal is 1100 1000 in binary; split in half, 1100 is C and 1000 is 8, so 200 is C8 in hex](https://cdn.hashnode.com/uploads/gql/6afe08476920da9a67541ede/54d522c7-f9b2-4bc7-a7dd-7e113b4fc34c.png align="center")

`11001000` → `1100` `1000` → `C` `8` → **C8**.

It works the other way too. `3C` → `3` is `0011`, `C` is `1100` → `00111100`.

## Hex to decimal

In a two-digit hex number, the left digit is worth **16 times** its value and the right digit is worth its own value. (Same idea as decimal, where the left digit of 42 is worth 10 times 4.)

- `C8` = 12 × 16 + 8 = 192 + 8 = **200**
- `1F` = 1 × 16 + 15 = **31**
- `FF` = 15 × 16 + 15 = 240 + 15 = **255**, the biggest number in one byte

For longer hex numbers, the third digit from the right is worth 256 times its value (16 × 16), but in day-to-day IT you'll mostly read hex one byte (two digits) at a time.

## Decimal to hex

Divide by 16. The whole number of times it fits is the left digit, and the remainder is the right digit.

- **200** ÷ 16 = 12, remainder 8. 12 is C. So 200 = **C8**.
- **172** ÷ 16 = 10, remainder 12. 10 is A, 12 is C. So 172 = **AC**.
- **16** ÷ 16 = 1, remainder 0. So 16 = **10** in hex.

## How hex is written

Because `10` could mean ten or sixteen, hex numbers often carry a marker:

- `0x` in front: `0xC8`, `0x80070005`. Common in programming, Windows error codes and tools.
- `#` in front, for web colors: `#39FF14`.
- Separators between bytes: `3C-52-82-1A-7F-09` (Windows MAC addresses) or `3c:52:82:1a:7f:09` (Linux and most network gear).

## Where you'll see hex

**MAC addresses.** A MAC address is 6 bytes, written as 6 pairs of hex digits. Run `ipconfig /all` and look at the **Physical Address** line. The first three bytes identify the company that made the network card (called the **OUI**, Organizationally Unique Identifier). You can look them up with [Wireshark's OUI lookup tool](https://www.wireshark.org/tools/oui-lookup.html), which helps when you find an unknown device on a network.

**Windows error codes.** Updates and apps fail with codes like `0x80070005` (access denied, usually a permissions problem). At a help desk, the hex code is the thing you search for, so copy it exactly.

**IPv6 addresses.** The newer version of IP uses 128-bit addresses written in hex, in eight groups of four digits, such as `2001:0db8:0000:0000:0000:0000:0000:0001`. Groups of zeros can be shortened, so that address is usually written `2001:db8::1`. (Addresses starting with `2001:db8` are reserved for examples and documentation, so it's safe to use in a post.)

**Colors.** Web colors are three bytes: red, green and blue. `#39FF14`, the green in Adam2Root's logo, is red `39` (57), green `FF` (255) and blue `14` (20). That's a lot of green and very little of the rest.

**What's inside a file.** Every file is bytes, and tools show those bytes in hex. Many file types start with the same few bytes, called a **file signature** or "magic bytes". A PNG image always starts with `89 50 4E 47`, a PDF with `25 50 44 46` (the text `%PDF`), and a ZIP file, including Word `.docx` files, with `50 4B` (the letters `PK`). Security analysts use this to spot a file that lies about its type, such as an "invoice.pdf" that's really a program.

You can look at a file's first bytes yourself in PowerShell:

```powershell
Format-Hex -Path .\photo.png | Select-Object -First 1
```

`Format-Hex` shows a file as hex, 16 bytes per line. `Select-Object -First 1` keeps only the first line. Replace `photo.png` with any image or PDF on your computer. The output looks like this:

```plaintext
00000000   89 50 4E 47 0D 0A 1A 0A 00 00 00 0D 49 48 44 52  ?PNG........IHDR
```

The left column is the position in the file (also in hex), the middle is the bytes, and the right side shows any bytes that happen to be readable letters.

## Tools that do it for you

**Windows Calculator, Programmer mode.** The same tool as in the binary post: Calculator > menu > **Programmer**. Type in DEC and read the HEX row, or click HEX and type hex (the letter buttons A to F become active).

**PowerShell:**

```powershell
'{0:X}' -f 200
```

Output: `C8`. `-f` is PowerShell's format operator, and `{0:X}` means "format the first value as upper-case hex".

```powershell
[Convert]::ToInt32("C8", 16)
```

Output: `200`. The `16` says the text is in base 16.

You can also just type a hex number with the `0x` marker and press Enter. PowerShell answers with the decimal value:

```powershell
0xC8
```

Output: `200`.

**Online:** RapidTables' [hex to decimal converter](https://www.rapidtables.com/convert/number/hex-to-decimal.html) shows the working, step by step.

## Practice with a game

![Flippy Bit and the Attack of the Hexadecimals from Base 16: the game screen with eight bit switches at the bottom](https://cdn.hashnode.com/uploads/gql/6afe08476920da9a67541ede/7e01b2bd-41f3-4e2d-a557-64c99daffb1e.png align="center")

[**Flippy Bit and the Attack of the Hexadecimals from Base 16**](https://flippybitandtheattackofthehexadecimalsfrombase16.com/) is a free browser game. Hex numbers fly toward you, and you shoot each one down by flipping the eight bits at the bottom to match it in binary. Click the bits or use the keyboard keys 1 to 8. It's fast and a bit silly, and after a few rounds you'll convert hex to binary without thinking. (The story on the page next to the game is an April Fools' joke, as the page itself admits. The game is real.)

## What to remember for now

- Hex digits go **0 to 9, then A to F** (A = 10, F = 15).
- **One hex digit is 4 bits. Two hex digits are one byte.**
- Convert through binary by splitting the byte in half, or with the "× 16 plus the remainder" method.
- `0x`, `#` and pairs separated by `-` or `:` all mean "this is hex".

You don't need to convert hex in your head at work. You do need to recognize it, read a MAC address or an error code correctly, and know where to convert it when you need to.

## Your exercise

1. Convert to hex by hand: **255**, **16**, **172**, **100**. Check in Calculator's Programmer mode.
2. Convert to decimal by hand: `1F`, `80`, `A0`, `FF`.
3. Convert to binary by splitting into halves: `3C`, `F0`.
4. Run `ipconfig /all`, find your Physical Address, and look up its first three bytes in [Wireshark's OUI lookup](https://www.wireshark.org/tools/oui-lookup.html). Does the maker match your network card?
5. Run `Format-Hex` on one PNG and one PDF from your computer. Do they start with the signatures from this post?
6. Play [Flippy Bit](https://flippybitandtheattackofthehexadecimalsfrombase16.com/) for ten minutes. Write down your score, and beat it in two days.

**Answers:** 255 = `FF`, 16 = `10`, 172 = `AC`, 100 = `64`. `1F` = 31, `80` = 128, `A0` = 160, `FF` = 255. `3C` = `00111100`, `F0` = `11110000`.

## Keep learning

- [**Math is Fun: Hexadecimals**](https://www.mathsisfun.com/hexadecimals.html)**.** A short, friendly explanation with a converter.
- [**Wikipedia: List of file signatures**](https://en.wikipedia.org/wiki/List_of_file_signatures)**.** The magic bytes of hundreds of file types. Bookmark it for CTFs.

## What's next

Now that you can read bytes in binary and hex, the next post covers encoding: how text becomes bytes (ASCII and UTF-8), what Base64 and URL encoding are, and why encoding something does not protect it.

What was your score in Flippy Bit? Tell me in the comments.

If you want to follow along, connect with me on [LinkedIn](https://www.linkedin.com/in/adammichalsiman/).
