Try it Live
Run Base64 examples in the interactive playground
Encoding
Methods for encoding binary data and strings to base64 format.Standard Base64 Encoding
- TypeScript
Base64.encode(data)
Encodes bytes to standard base64 string using RFC 4648 alphabet (A-Z, a-z, 0-9, +, /) with padding.data: Uint8Array- Binary data to encode
Base64String - Standard base64-encoded string with paddingExample:Base64.encodeString(str)
Encodes UTF-8 string to standard base64. Internally converts string to bytes using TextEncoder, then encodes to base64.
str: string- UTF-8 string to encode
Base64String - Base64-encoded string
Example:
URL-Safe Base64 Encoding
- TypeScript
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
data: Uint8Array- Binary data to encode
Base64UrlString - URL-safe base64 string without paddingExample:Base64.encodeStringUrlSafe(str)
Encodes UTF-8 string to URL-safe base64. Combines TextEncoder with URL-safe base64 encoding.
str: string- UTF-8 string to encode
Base64UrlString - URL-safe base64 string
Example:
Usage Patterns
Encoding with Size Calculation
Choosing Standard vs URL-Safe
Encoding Large Data
Binary Data Encoding
Encoding Algorithm
Standard base64 encoding process:- Group input: Split input bytes into 3-byte groups
- Convert to base64: Each 3-byte group becomes 4 base64 characters
- Padding: If final group has fewer than 3 bytes, pad with
=
Visual: 3 Bytes → 4 Characters
Complete Example: “Hello” → “SGVsbG8=”
- Input length % 3 == 0: No padding (
SGVs) - Input length % 3 == 1: Add
==(SGVsbG8=) - Input length % 3 == 2: Add
=(SGVsbGk=)
Related
- Decoding - Decoding base64 strings
- Validation - Validating base64 format
- Utilities - Size calculations
- BrandedBase64 - Tree-shakeable API

