Skip to main content
Binary, Hexadecimal & Text Encoding
Lesson 6 of 10

Binary, Hexadecimal & Text Encoding

Learn how computers represent data in binary and hexadecimal, how ASCII and UTF-8 map bytes to text, and how to decode Base64 and hex safely in a lab.

Beginner 20 min IT foundations

Learning objectives

  • Convert small values between binary, decimal and hexadecimal by grouping four bits per hex digit
  • Explain how ASCII and UTF-8 encode characters into one or more bytes
  • Identify Base64 and hexadecimal encodings in logs or lab files and decode them safely with Python or xxd
  • Distinguish reversible data encoding from encryption and cryptographic hashing

Recommended first: CPU, Memory & Program ExecutionCommand Line & Shell Workflow

#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 0100 equals 4.
  • Right nibble 1001 equals 8 + 1 = 9.
  • Combined, 01001001 is 0x49 (decimal 4 * 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:

Inspecting bytes with xxd in a lab shell
$ 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).
python
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.

  1. Raw bytes (3 x 8 bits)Split into four 6-bit groups
  2. Map to A-Z a-z 0-9 + /Safe ASCII text (+ '=' padding)
  3. 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 '.

Encoding and decoding safely in Python 3
$ 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 bash or eval(); decode to text or inspect bytes first.
  • Mixing str and bytes in Python 3: Text (str) must be encoded (s.encode("utf-8")) before passing to base64 or .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 xxd and Python's bytes.fromhex and base64 module to inspect encoded strings passively before trusting them.

Knowledge check

Sign in to take the test, complete lessons and save your progress. Log in

Practice: test your knowledge