Skip to content
Decode: 4 Chars to 3 Bytes
step 1/5

Reading — step 1 of 5

Read

~1 min readDecoding

Decode: 4 Chars to 3 Bytes

Reverse of encoding. For each 4-char chunk:

  1. Map each char to its 6-bit value (or 0 if =).
  2. Combine into a 24-bit integer.
  3. Split back into 3 bytes.
  4. Trim trailing bytes per the = count.
python

Validation:

  • Input length must be multiple of 4 (after padding)
  • Only valid alphabet chars (or =)
  • = only allowed at the end (1 or 2 of them)
  • Stricter validators check that the trailing 6-bit value's "ignored" bits are zero (since they padded with zero bytes)

Most implementations are lenient: accept whitespace (newlines especially — PEM files have line breaks every 64 or 76 chars), ignore unknown chars, etc.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…