#Why this matters
As seen when studying CPU and memory execution, computers store every file, image, packet and program instruction as sequences of 0s and 1s. Reading long rows of binary digits is slow and error-prone, and sending raw binary bytes through text-based channels like HTTP headers or email often corrupts the data.
To solve both problems, engineers and security analysts rely on standard representations: hexadecimal for compact byte inspection, ASCII and UTF-8 for text, and Base64 for transporting binary safely as printable characters. These same formats appear daily in log analysis, packet captures and beginner misc CTF challenges.
#Bits, bytes and hexadecimal
A bit holds one value (0 or 1). Eight bits form a byte, which can hold 2^8 = 256 values from 0 to 255 in decimal. Four bits form a nibble (0 to 15). Because 16 is a power of two, hexadecimal (base 16) maps one hex digit (0-9 and a-f) cleanly to one nibble, and two hex digits (00 to ff) to one byte.
| Decimal | 8-bit binary | Hexadecimal | Meaning in ASCII |
|---|---|---|---|
10 |
00001010 |
0x0a |
Newline (\n) |
32 |
00100000 |
0x20 |
Space ( ) |
65 |
01000001 |
0x41 |
Uppercase A |
97 |
01100001 |
0x61 |
Lowercase a |
255 |
11111111 |
0xff |
Not valid 7-bit ASCII |
To convert a byte from binary to hex by hand, split it into two 4-bit halves. For 01001001:
- Left nibble
0100equals4. - Right nibble
1001equals8 + 1 = 9. - Combined,
01001001is0x49(decimal4 * 16 + 9 = 73, which is'I'in ASCII).
On the command line, xxd prints a file's raw bytes as hex alongside their printable ASCII view, while xxd -p outputs plain hex:
$ printf 'Hi!' | xxd -p
486921
$ printf '486921' | xxd -r -p
Hi!Here, H is 0x48, i is 0x69 and ! is 0x21. Non-printable bytes show up as dots (.) in standard xxd output so the terminal layout does not break.
#Text encoding: ASCII and UTF-8
A character encoding is an agreed table mapping characters to numbers. ASCII uses 7 bits (0x00 to 0x7f, 128 values). Printable ASCII covers 0x20 (space) through 0x7e (~). Notice a helpful pattern: uppercase 'A' (0x41) and lowercase 'a' (0x61) differ by 0x20 (32), and digits '0'-'9' sit at 0x30-0x39.
ASCII cannot represent accented letters, Cyrillic, Arabic or mathematical symbols. UTF-8 solves this by using 1 to 4 bytes per character:
- Any ASCII character (
0x00-0x7f) keeps the exact same single byte in UTF-8. - Characters beyond ASCII use multi-byte sequences whose bytes start with high bits set (
>= 0x80).
word = "caf\u00e9"
raw = word.encode("utf-8")
print(len(word), len(raw), raw.hex())Running this snippet prints 4 5 636166c3a9. The word has 4 characters, yet UTF-8 produces 5 bytes because the accented e takes two bytes (c3 a9). Assuming that string length equals byte length is a classic parsing bug.
#Transport encoding: Base64 and URL encoding
When binary bytes or special characters must pass through a text-only system (such as JSON, email or a URL), applications use transport encodings.
Base64 takes 3 bytes (24 bits) at a time and splits them into four 6-bit chunks. Each 6-bit value (0 to 63) maps to a character in the Base64 alphabet (A-Z, a-z, 0-9, +, /). If the input byte count is not divisible by 3, one or two = characters pad the end. That = suffix is a quick visual clue, though Base64 strings whose length is a multiple of 3 bytes have no padding at all.
- Raw bytes (3 x 8 bits)Split into four 6-bit groups
- Map to A-Z a-z 0-9 + /Safe ASCII text (+ '=' padding)
- Decode in Python base64Original raw bytes restored
URL (percent) encoding replaces unsafe characters in web addresses with % followed by two hex digits, such as %20 for space (0x20) or %27 for '.
$ python3 -c "import base64; print(base64.b64encode(b'Hi!').decode())"
SGkh
$ python3 -c "import base64; print(base64.b64decode('ZWNobyBsYWItY2hlY2s=').decode('utf-8'))"
echo lab-check#Common mistakes
- Treating encoding as security: Base64 and hex provide readability and safe transport, not confidentiality.
- Confusing encoding with hashing: Encodings are two-way and reversible; cryptographic hashes such as SHA-256 are one-way digests.
- Executing decoded payloads blindly: Never pipe unknown Base64 output straight into
bashoreval(); decode to text or inspect bytes first. - Mixing
strandbytesin Python 3: Text (str) must be encoded (s.encode("utf-8")) before passing tobase64or.hex(), and decoded bytes should be checked before printing as text.
#Summary
- Binary stores data as 0s and 1s, while hexadecimal groups 4 bits per digit so two hex digits equal one byte.
- ASCII encodes 128 basic characters in 7 bits; UTF-8 extends this to all Unicode characters using 1 to 4 bytes while keeping ASCII unchanged.
- Base64 and URL encoding make arbitrary bytes safe for text channels, using no key and providing zero secrecy.
- Use
xxdand Python'sbytes.fromhexandbase64module to inspect encoded strings passively before trusting them.