Encode / DecodeBlur image

What encoding means#

Encoding turns information into a format that can be stored or transmitted, and decoding turns it back. The idea is everywhere: Morse code encodes letters, a barcode encodes a number, and even DNA encodes proteins in three-letter groups that cells decode. In computing everything ends up as bytes, and an encoding is simply the agreement about what those bytes mean.

Encoding, encryption and hashing are not the same#

TypePurposeReversible?Needs a key?Examples
EncodingRepresent data in a formatYesNoUTF-8, Base64, URL encoding
EncryptionKeep data secretYes, with the keyYesAES, GPG (see Password manager and easy encryption)
HashingFingerprint data, check integrityNoNoSHA-256

The most common mistake is treating an encoding as protection: anyone can decode Base64, so it hides nothing.

Formats you meet every day#

  • UTF-8 is how text becomes bytes. A letter takes one to four bytes: A is 41, é is C3 A9, € is E2 82 AC and 😀 is F0 9F 98 80. Read those bytes with the wrong encoding and you get the classic garbled text: café shown as café.
  • Base64 carries binary data inside plain text (e-mail attachments, data: URLs, JSON, tokens). Every 3 bytes become 4 printable characters, so the result is about a third bigger. Hello becomes SGVsbG8=.
  • URL (percent) encoding replaces unsafe characters with % and the byte in hexadecimal: café & té becomes caf%C3%A9%20%26%20t%C3%A9.
  • Hexadecimal and binary are just other ways to write bytes: Hi is 48 69 in hex and 01001000 01101001 in binary.

Try it yourself#

In a Linux or macOS terminal:

echo -n "Hello" | base64        # SGVsbG8=
echo "SGVsbG8=" | base64 -d     # Hello
bash

In PowerShell:

[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("Hello"))
# SGVsbG8=
[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String("SGVsbG8="))
# Hello
powershell

In Python:

import base64, urllib.parse

base64.b64encode("café".encode("utf-8")).decode()   # 'Y2Fmw6k='
urllib.parse.quote("café & té")                      # 'caf%C3%A9%20%26%20t%C3%A9'
python

To experiment without writing code, CyberChef ↗ runs in the browser and chains many encodings and decodings.

Common mistakes#

  • Using Base64 as encryption. It is reversible by anyone; use real encryption if the data is secret.
  • Encoding twice. A %20 that gets encoded again becomes %2520.
  • Not stating the encoding. Save files as UTF-8, declare <meta charset="utf-8"> in web pages and, in Python, open text files with encoding="utf-8" instead of relying on the system default (on Windows it is often a legacy code page).

In short#

Encoding is only a format agreement: it makes data portable, not secret. Knowing which one you are looking at (UTF-8, Base64, percent-encoding, hex) is most of the work when something looks garbled.

Encode / Decode
https://david7ce.github.io/en/post/encode-decode/
AuthorDavid7ce
Published atOctober 26, 2023