Skip to main content

Try it Live

Run Base64 examples in the interactive playground

Encoding

Methods for encoding binary data and strings to base64 format.

Standard Base64 Encoding

Base64.encode(data)

Encodes bytes to standard base64 string using RFC 4648 alphabet (A-Z, a-z, 0-9, +, /) with padding.
Parameters:
  • data: Uint8Array - Binary data to encode
Returns: Base64String - Standard base64-encoded string with paddingExample:
Defined in: primitives/Base64/BrandedBase64/encode.js:17

Base64.encodeString(str)

Encodes UTF-8 string to standard base64. Internally converts string to bytes using TextEncoder, then encodes to base64.
Parameters:
  • str: string - UTF-8 string to encode
Returns: Base64String - Base64-encoded string Example:
Defined in: primitives/Base64/BrandedBase64/encodeString.js:14

URL-Safe Base64 Encoding

Base64.encodeUrlSafe(data)

Encodes bytes to URL-safe base64 string. Uses URL-safe alphabet (A-Z, a-z, 0-9, -, _) and removes padding characters.Differences from standard base64:
  • Replaces + with -
  • Replaces / with _
  • Removes = padding
Parameters:
  • data: Uint8Array - Binary data to encode
Returns: Base64UrlString - URL-safe base64 string without paddingExample:
Defined in: primitives/Base64/BrandedBase64/encodeUrlSafe.js:19

Base64.encodeStringUrlSafe(str)

Encodes UTF-8 string to URL-safe base64. Combines TextEncoder with URL-safe base64 encoding.
Parameters:
  • str: string - UTF-8 string to encode
Returns: Base64UrlString - URL-safe base64 string Example:
Defined in: primitives/Base64/BrandedBase64/encodeStringUrlSafe.js:9

Usage Patterns

Encoding with Size Calculation

Choosing Standard vs URL-Safe

Encoding Large Data

Binary Data Encoding

Encoding Algorithm

Standard base64 encoding process:
  1. Group input: Split input bytes into 3-byte groups
  2. Convert to base64: Each 3-byte group becomes 4 base64 characters
  3. Padding: If final group has fewer than 3 bytes, pad with =

Visual: 3 Bytes → 4 Characters

Complete Example: “Hello” → “SGVsbG8=”

Padding Rules:
  • Input length % 3 == 0: No padding (SGVs)
  • Input length % 3 == 1: Add == (SGVsbG8=)
  • Input length % 3 == 2: Add = (SGVsbGk=)
URL-safe encoding applies character substitutions after standard encoding: