CoderTools

Base64 Encoder/Decoder

Quickly encode and decode Base64 strings, supporting both text and file conversion

Charset

What is Base64?

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).

Base64 Encoding Algorithm Principle:

The Base64 encoding process can be divided into the following steps:

  1. Split the input data into groups of 3 bytes each
  2. Rearrange these 3 bytes (24 bits) into 4 groups of 6 bits each
  3. Map each 6-bit value (range 0-63) as an index to the Base64 character set
  4. If the last group has fewer than 3 bytes, fill with 0 bits and add the corresponding number of equal signs (=) as padding at the end of the result

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;
}

Common Uses:

  • Encoding email attachments (MIME standard)
  • Securely transmitting data in URLs (using Base64url variant, where '+' and '/' are replaced with '-' and '_')
  • Embedding binary images in HTML or CSS (data URI scheme)
  • Storing simple data as a single string (such as JWT token components)
  • Including binary data in XML and JSON (avoiding special character escaping issues)

Advanced Features

Hexadecimal Format Support

Our tool supports hexadecimal format input and output, useful for developers working with binary data in program code.

  • Input: Accept hex format like 0x11,0x22,0x33 or simple 112233
  • Output: Convert decoded Base64 back to hex format for code embedding

Character Set Support

Different character sets encode text differently into bytes. Our tool supports multiple character sets to handle various text encodings:

  • UTF-8: Universal encoding supporting all Unicode characters (default)
  • ASCII: 7-bit encoding supporting basic English characters (0-127)
  • Latin-1 (ISO-8859-1): 8-bit encoding supporting Western European characters (ISO-8859-1)
  • UTF-16: 16-bit encoding commonly used by Windows and Java (Little Endian)

Important Notes:

  • Base64 encoded data is typically about 33% larger than the original data (because 3 bytes become 4 characters)
  • Base64 is not an encryption algorithm, just an encoding method, and provides no security
  • Base64 encoding of large files can consume significant memory and processing time
  • URL-safe Base64 variants use different character sets to avoid URL encoding issues
  • Standard Base64 may include line breaks to limit line length, which requires additional handling in some applications

Comparison of Base64 with Other 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)