How to Encode and Decode Base64 Data
Base64 converts binary data to ASCII text for safe transmission. Learn how Base64 encoding works and its common uses in web development.
Key Takeaways
- Base64 encodes binary data using 64 ASCII characters (A-Z, a-z, 0-9, +, /).
- Data URIs**: Embedding images directly in HTML/CSS as `data:image/png;base64,...`
- Standard Base64 uses `+` and `/` which are special characters in URLs.
- Decoding reverses the process, converting the ASCII text back to binary.
Hash Generator
Generate SHA-1, SHA-256, SHA-384, SHA-512 hashes from text
What Is Base64?
Base64 encodes binary data using 64 ASCII characters (A-Z, a-z, 0-9, +, /). It's used to embed binary data in text-based formats like JSON, XML, HTML, and email. The output is ~33% larger than the input.
Common Use Cases
- Data URIs: Embedding images directly in HTML/CSS as
data:image/png;base64,... - Email attachments: MIME encoding uses Base64 for binary attachments.
- API payloads: Sending binary files in JSON API requests.
- JWT tokens: Header and payload are Base64URL-encoded.
Base64 vs Base64URL
Standard Base64 uses + and / which are special characters in URLs. Base64URL replaces these with - and _, making encoded data safe for URLs and filenames. JWT tokens and URL parameters use Base64URL.
Performance Considerations
Base64 adds 33% overhead. A 1MB image becomes 1.33MB when Base64-encoded. For small inline images (under 10KB), the overhead is acceptable. For larger files, separate file uploads are more efficient.
Decoding
Decoding reverses the process, converting the ASCII text back to binary. If the Base64 string is corrupted (wrong length, invalid characters), decoding fails. Common issues include missing padding (=) characters.
संबंधित टूल्स
संबंधित फ़ॉर्मेट
संबंधित गाइड
JSON vs YAML vs TOML: Choosing a Configuration Format
Configuration files are the backbone of modern applications. JSON, YAML, and TOML each offer different trade-offs between readability, complexity, and tooling support that affect your development workflow.
How to Format and Validate JSON Data
Malformed JSON causes silent failures in APIs and configuration files. Learn how to format, validate, and debug JSON documents to prevent integration errors and improve readability.
Base64 Encoding: How It Works and When to Use It
Base64 converts binary data into ASCII text, making it safe for transmission through text-based systems. Learn when Base64 is the right choice and when alternatives like hex encoding or URL encoding are more appropriate.
Best Practices for Working with Unix Timestamps
Unix timestamps provide a language-agnostic way to represent points in time, but they come with pitfalls around time zones, precision, and the 2038 problem. This guide covers best practices for storing and converting timestamps.
Troubleshooting JWT Token Issues
JSON Web Tokens are widely used for authentication but can be frustrating to debug. This guide covers common JWT problems including expiration errors, signature mismatches, and payload decoding issues.