# Encoding Explained: ASCII, UTF-8, Base64 and URL Encoding for Beginners

A computer only stores numbers, so how does it store the letter "A"? Someone had to agree on a rule: A is 65, B is 66, and so on. A rule like that, for turning data into a different form and back, is called an **encoding**. You meet encodings every day in IT. They're behind the email with strange characters like `Ã©` instead of `é`, the link full of `%20`, and the long string ending in `==` that turns out to be a hidden message in a CTF (a hacking practice challenge). This post covers the four encodings you'll see most, how to recognize each one, and how to decode them with free tools.

One thing to remember before we start: **encoding is not security**. Anyone can reverse it, without a password or a key. That point comes back at the end.

## ASCII: one number per character

**ASCII** (American Standard Code for Information Interchange) is the oldest encoding still in daily use. It gives a number to 128 characters: English letters, digits, punctuation and a few control codes. Some examples:

| Character | Decimal | Hex | Binary |
| --- | --- | --- | --- |
| `A` | 65 | 41 | 01000001 |
| `a` | 97 | 61 | 01100001 |
| `0` (the digit) | 48 | 30 | 00110000 |
| space | 32 | 20 | 00100000 |

You don't need to memorize the table. Notice two things: upper and lower case are different numbers, and the digit "0" is not the number 0. To a computer, "A" and "a" are different characters, which is one reason passwords are case sensitive.

PowerShell can show you the code of any character, and the character for any code:

```powershell
[int][char]'A'
```

Output: `65`. `[char]` treats `'A'` as a single character, and `[int]` turns it into its number.

```powershell
[char]65
```

Output: `A`.

Here's one short word, written five different ways. It's the same two bytes each time.

![The word Hi written five ways: as text, as ASCII codes 72 and 105, as hex 48 69, as binary 01001000 01101001, and as Base64 SGk=](https://cdn.hashnode.com/uploads/gql/6afe08476920da9a67541ede/8299eaba-57e8-4fe8-ac91-7f686232ca02.png align="center")

## UTF-8: every language and emoji

ASCII only covers English. **Unicode** is the modern standard that gives a number to every character in every writing system, plus emoji: over 150,000 of them. **UTF-8** is the most common way to store those numbers as bytes. It's what nearly every website uses.

UTF-8 keeps the ASCII characters as one byte each, exactly as before. Other characters take two to four bytes. The letter `é` takes two: `C3 A9`.

That explains a ticket you'll see at a help desk: text full of characters like `Ã©`, `â€™` or `Â`. The text was saved as UTF-8 but opened by a program that read it as an older encoding, one byte per character. The two bytes of `é` get shown as two separate characters, `Ã` and `©`. The fix is to open or import the file as UTF-8, usually a setting in the program's open or import dialog (Excel's import from CSV has one, for example). Nothing is corrupted. It's being read with the wrong rule.

## Base64: binary data as plain text

Some systems were built to carry only plain text: email, web addresses, many configuration files. To send an image or any other binary data through them, you need to turn the bytes into safe, printable characters. **Base64** does that.

It uses 64 characters: `A-Z`, `a-z`, `0-9`, `+` and `/`. Every 3 bytes of data become 4 Base64 characters. If the data doesn't divide evenly by 3, the output is padded with one or two `=` signs at the end. So the result is always about a third bigger than the original.

You'll find Base64 in:

- **Email attachments**, which travel inside the message as Base64.
- **Web pages**, where small images are sometimes embedded directly as Base64.
- **Login tokens and cookies**, such as the JWT tokens you'll meet in web hacking.
- **CTF challenges**, constantly. If a string ends in `=` or `==`, try Base64 first.

Encode and decode Base64 in PowerShell:

```powershell
[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("Hello"))
```

Output: `SGVsbG8=`. Read it from the inside out: `GetBytes` turns the text into UTF-8 bytes, and `ToBase64String` turns those bytes into Base64.

```powershell
[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String("SGVsbG8="))
```

Output: `Hello`. The same steps in reverse.

For whole files, Windows has a built-in tool:

```cmd
certutil -encode photo.png photo-b64.txt
certutil -decode photo-b64.txt photo-copy.png
```

`certutil` was made for certificates, so it wraps the output in `-----BEGIN CERTIFICATE-----` lines even when the file isn't a certificate. That's normal.

On Linux (you'll set it up later in the course), it's one short command each way: `echo -n "Hello" | base64` and `echo "SGVsbG8=" | base64 -d`.

The easiest tool of all is [**CyberChef**](https://gchq.github.io/CyberChef/), a free web app made by GCHQ, the UK's intelligence and security agency. It runs in your browser. You build a "recipe" by dragging operations into the middle column. Paste the input at the top right and the output appears below it.

![CyberChef decoding the Base64 string SGVsbG8gZnJvbSBBZGFtMlJvb3Qh with the From Base64 operation, giving Hello from Adam2Root!](https://cdn.hashnode.com/uploads/gql/6afe08476920da9a67541ede/f9efcacd-e5e7-4b4f-b817-e78715ae58a0.png align="center")

CyberChef runs on your computer, in the browser, so the data you paste doesn't leave your machine. Even so, at work, don't paste real passwords or customer data into any website.

## URL encoding: safe characters in web addresses

Web addresses can only contain certain characters. Spaces aren't allowed, and characters like `&`, `?` and `/` have special jobs in a URL. **URL encoding** (also called **percent-encoding**) replaces each problem character with `%` followed by its byte in hex:

- space → `%20`
- `&` → `%26`
- `?` → `%3F`

So a search for `cats & dogs?` becomes `cats%20%26%20dogs%3F`. In search boxes and forms you'll also see a space written as `+`.

In PowerShell:

```powershell
[uri]::EscapeDataString("cats & dogs?")
```

Output: `cats%20%26%20dogs%3F`.

```powershell
[uri]::UnescapeDataString("cats%20%26%20dogs%3F")
```

Output: `cats & dogs?`.

You'll use URL encoding a lot in web application testing later in the course, because what you type into a website often travels inside a URL.

## Spot the encoding

Recognizing an encoding is half the job. These clues work almost every time:

![How to spot four encodings: binary uses only 0 and 1, hex uses 0-9 and a-f, Base64 uses letters, digits, + and / and often ends in =, URL encoding uses % followed by two hex digits](https://cdn.hashnode.com/uploads/gql/6afe08476920da9a67541ede/410d9683-088f-40b8-9b05-ec6e36257adc.png align="center")

When you're not sure, CyberChef has an operation called **Magic** that tries common decodings for you and shows which ones produce readable text.

## Encoding is not security

Encoding hides nothing. It changes the format, and anyone can change it back without a key or password. Plenty of real systems still treat it as if it protected something, and it doesn't.

A classic example is **HTTP Basic authentication**, an old way for websites to ask for a login. The browser sends the username and password in a header like this:

```plaintext
Authorization: Basic dXNlcjpwYXNz
```

It looks scrambled. Decode `dXNlcjpwYXNz` from Base64 and you get `user:pass`. That's why a login like this is only safe over HTTPS, where the whole connection is encrypted. (Encryption, which really does need a key, gets its own post soon.)

As a rule: if a password in a config file or a script is only Base64-encoded, treat it as written in plain text. At a help desk, that's worth reporting. In a penetration test, it's a finding.

## What to remember for now

- An **encoding** is a public rule for changing the form of data. No key, and anyone can reverse it.
- **ASCII**: one number per English character. **UTF-8**: every language, with ASCII unchanged.
- **Base64**: letters, digits, `+` and `/`, often ends in `=`. Used to carry binary data as text.
- **URL encoding**: `%` plus two hex digits, such as `%20` for a space.
- Encoding is not security.

## Your exercise

1. Use PowerShell to find the ASCII codes of `I` and `T`. Then write them in hex.
2. Decode this Base64 string, in PowerShell or CyberChef: `SGVsbG8gZnJvbSBBZGFtMlJvb3Qh`
3. Encode your own first name in Base64, then decode it back.
4. Decode this URL-encoded search: `help%20desk%20jobs%26salary`
5. Decode this HTTP Basic login header: `Basic dXNlcjpwYXNz`
6. **CTF-style challenge.** This string is encoded twice. Find out how, and decode it to get the flag:

```plaintext
NjY2YzYxNjc3YjY1NmU2MzZmNjQ2OTZlNjc1ZjY5NzM1ZjZlNmY3NDVmNjU2ZTYzNzI3OTcwNzQ2OTZmNmU3ZA==
```

Hint: what does the end of the string tell you? And after the first step, which characters do you see?

**Answers:** `I` = 73 (hex 49), `T` = 84 (hex 54). Step 2: `Hello from Adam2Root!` Step 4: `help desk jobs&salary`. Step 5: `user:pass`. Step 6: the `==` says Base64. Decoding it gives a long string of only 0-9 and a-f, which is hex. Decode the hex (CyberChef: From Base64, then From Hex) and you get `flag{encoding_is_not_encryption}`.

## Keep learning

- [**CyberChef**](https://gchq.github.io/CyberChef/)**.** Bookmark it. You'll use it for the rest of the course and in every CTF.
- [**MDN: Base64**](https://developer.mozilla.org/en-US/docs/Glossary/Base64)**.** A short, clear definition from Mozilla's web documentation.
- [**MDN: Percent-encoding**](https://developer.mozilla.org/en-US/docs/Glossary/Percent-encoding)**.** The full list of characters that get URL-encoded.
- [**RapidTables: ASCII, hex, binary and decimal converter**](https://www.rapidtables.com/convert/number/ascii-hex-bin-dec-converter.html)**.** Type text and see it in every format at once.
- [**pwn.college: Data Dealings**](https://pwn.college/fundamentals/data-dealings/)**.** A free set of challenges from Arizona State University on hex, ASCII, UTF-8 and nested encodings. It runs in a Linux workspace in your browser and uses a little Python, so it's harder than this post. Try it now if you're curious, or come back after the Linux posts.

## What's next

Encoding changes data and can always be reversed. The next post is about hashing, which does the opposite: it turns any data into a short fingerprint that can't be turned back. It's how websites store your password, and how you check that a download hasn't been tampered with.

Did you solve the double-encoding challenge? Tell me in the comments how you spotted the first step, but don't post the flag.

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