Skip to main content

Try it Live

Run Base64 examples in the interactive playground

Decoding

Methods for decoding base64 strings to binary data and UTF-8 strings.

Standard Base64 Decoding

Base64.decode(encoded)

Decodes standard base64 string to bytes. Accepts strings encoded with RFC 4648 alphabet (A-Z, a-z, 0-9, +, /) with padding.
Parameters:
  • encoded: string - Base64 string to decode
Returns: Uint8Array - Decoded binary dataThrows:
  • Error - If input is invalid base64 format
Example:
Defined in: primitives/Base64/BrandedBase64/decode.js:14

Base64.decodeToString(encoded)

Decodes base64 string directly to UTF-8 string. Combines base64 decoding with TextDecoder for convenience.
Parameters:
  • encoded: string - Base64 string to decode
Returns: string - Decoded UTF-8 string Throws:
  • Error - If input is invalid base64 format
Example:
Defined in: primitives/Base64/BrandedBase64/decodeToString.js:15

URL-Safe Base64 Decoding

Base64.decodeUrlSafe(encoded)

Decodes URL-safe base64 string to bytes. Accepts strings encoded with URL-safe alphabet (A-Z, a-z, 0-9, -, _) without padding.Automatically handles:
  • Missing padding characters
  • URL-safe character substitutions (- → +, _ → /)
Parameters:
  • encoded: string - URL-safe base64 string (without padding)
Returns: Uint8Array - Decoded binary dataThrows:
  • Error - If input is invalid base64 format
Example:
Defined in: primitives/Base64/BrandedBase64/decodeUrlSafe.js:10

Base64.decodeUrlSafeToString(encoded)

Decodes URL-safe base64 string directly to UTF-8 string. Combines URL-safe decoding with TextDecoder.
Parameters:
  • encoded: string - URL-safe base64 string
Returns: string - Decoded UTF-8 string Throws:
  • Error - If input is invalid
Example:
Defined in: primitives/Base64/BrandedBase64/decodeUrlSafeToString.js:9

Usage Patterns

Safe Decoding with Validation

Decoding with Size Calculation

Auto-detecting Format

Streaming Decoding

JSON Decoding

Error Handling

Decoding Binary Types

Decoding Algorithm

Standard base64 decoding process:
  1. Validate format: Check for valid base64 characters and padding
  2. Remove padding: Strip trailing = characters
  3. Convert from base64: Each 4 base64 characters become 3 bytes
  4. Return bytes: Construct Uint8Array from decoded data

Visual: 4 Characters → 3 Bytes

Complete Example: “SGVsbG8=” → “Hello”

Padding Detection:
  • == (2 padding): Lost 2 bits, actual length -2 bytes
  • = (1 padding): Lost 1 bit, actual length -1 byte
  • No padding: Complete 3-byte group
Length Formula:
  • Output length = floor(input.length * 3 / 4) - padding_count
URL-safe decoding performs character substitutions before standard decoding: