Quickly encode and decode Base64 strings, supporting both text and file conversion
Base64 is an encoding scheme that represents binary data using 64 printable ASCII characters. These characters include uppercase and lowercase letters, numbers, and some special symbols. Base64 is widely used in scenarios where binary data needs to be transmitted through text-only channels, such as email attachments and URL parameters. The Base64 encoding scheme was originally defined in RFC 4648 and the MIME specification (RFC 2045).
The Base64 encoding process can be divided into the following steps:
Base64 Character Set: A-Z, a-z, 0-9, +, /
Padding Character: =
Conversion Example:
ASCII code for text "Man": 77 97 110
Binary representation: 01001101 01100001 01101110
Regrouped into 6 bits: 010011 010110 000101 101110
Decimal values: 19 22 5 46
Base64 result: T W F u
Simplified JavaScript Implementation:
// Base64 encoding implementation example (simplified)
function base64Encode(str) {
// Base64 character set
const base64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
let result = '';
let i = 0;
// Process every 3 characters as a group
while (i < str.length) {
const char1 = str.charCodeAt(i++);
const char2 = i < str.length ? str.charCodeAt(i++) : 0;
const char3 = i < str.length ? str.charCodeAt(i++) : 0;
// Convert 3 8-bit bytes into 4 6-bit indices
const triplet = (char1 << 16) + (char2 << 8) + char3;
// Extract 4 6-bit values from the triplet
const index1 = (triplet >> 18) & 0x3F;
const index2 = (triplet >> 12) & 0x3F;
const index3 = (triplet >> 6) & 0x3F;
const index4 = triplet & 0x3F;
// Add padding based on the original data length
if (i - 3 > str.length) {
result += base64chars[index1] + base64chars[index2] + '==';
} else if (i - 2 > str.length) {
result += base64chars[index1] + base64chars[index2] + base64chars[index3] + '=';
} else {
result += base64chars[index1] + base64chars[index2] + base64chars[index3] + base64chars[index4];
}
}
return result;
}
Our tool supports hexadecimal format input and output, useful for developers working with binary data in program code.
Different character sets encode text differently into bytes. Our tool supports multiple character sets to handle various text encodings:
Encoding Type | Characteristics | Main Uses |
---|---|---|
Base64 | Uses 64 ASCII characters to represent binary data | Email attachments, binary data transmission in text |
URL Encoding | Converts special characters to %XX format | URL parameter passing, form submission |
Hex Encoding | Each byte is represented as two hexadecimal characters | Hash value representation, binary data visualization |
For more official information about Base64, please refer to the following resources: RFC 4648 (Base64 Standard) | Wikipedia: Base64 | RFC 2045 (MIME Specification)