Waiting for input

Input

Output

 

The result appears here as you type.

    Ctrl+Enter run / or Ctrl+K search tools Press Ctrl+D to bookmark this page

    How to encode text or a file to Base64

    1. Type or paste text into the Input box. It is encoded as UTF-8 as you type.
    2. For a file, press Encode a file or drop it onto the input. Files up to 50 MB are read in your browser and encoded in a background thread, with progress and Cancel.
    3. Choose the options the receiving system expects (see the table below). The defaults, standard Base64 on one line with padding, suit most uses.
    4. Copy the result or download it as a text file.

    Have Base64 to read? Press Decode, or open the Base64 decoder.

    Which options to choose

    OptionUsed forWhat it does
    StandardJSON, email, most APIsUses + and /, with = padding
    URL-safeURLs, file names, JWTsUses - and _; padding is often left out
    Every 76 (MIME)Email attachmentsLines of 76 characters, usually with CRLF
    Every 64 (PEM)Certificates and keysLines of 64 characters between BEGIN and END lines
    Data URLHTML, CSS, small imagesdata:<type>;base64, followed by one line of standard Base64

    Data URLs always use standard Base64 with padding and no line breaks, so those options switch off while it is on.

    Text is encoded as UTF-8

    Base64 encodes bytes, so text is first written as UTF-8 bytes. That is what nearly every system expects, and it means every language round-trips exactly: Café becomes Q2Fmw6k=, the Tamil word தமிழ் becomes 4K6k4K6u4K6/4K604K+N, and 👋 becomes 8J+Riw==. The browser's own btoa() cannot do this on its own: it throws an error on any character outside Latin-1.

    Making a data URL

    Encode a file and tick Output as data URL. The type is taken from the file's first bytes, so a PNG renamed to .txt still becomes data:image/png;base64,…. If the type is not recognised, the URL uses application/octet-stream rather than a guess.

    Data URLs suit small things such as icons. Because Base64 is a third larger than the file and cannot be cached on its own, larger images load faster as normal files.

    How much bigger the result is

    Every 3 bytes become 4 characters, so Base64 is about 33% larger than the original, plus 1 to 3% for line breaks. The summary shows the exact sizes. Very long results are shown in part to keep the page fast; Copy and Download always use the full result.

    Your files stay on your device

    Text and files are encoded in your browser and never uploaded. This tool saves nothing, not even your last input, and never puts your input in a link.

    Frequently asked questions

    Which character encoding is used for text?

    UTF-8, which almost every system expects. Accented letters, Tamil, Chinese, emoji and every other character encode and decode back exactly.

    When do I need line breaks?

    Email attachments (MIME) use lines of 76 characters with CRLF line endings, and PEM files such as certificates use 64. JSON, URLs, HTTP headers and most APIs want one unbroken line, which is the default.

    How do I make a data URL from an image?

    Choose Encode a file, pick the image and tick Output as data URL. The type in the data URL comes from the file's first bytes, not its name, so a renamed file still gets the right type.

    Should I put images in my pages as Base64?

    Only small ones, such as icons under a few kilobytes. Base64 is a third larger than the file and cannot be cached on its own, so larger images load faster as normal files.

    Encode Base64 in your code

    Each example encodes UTF-8 text as standard and URL-safe Base64 and decodes it back, using only the standard library.

    Java

    import java.io.PrintStream;
    import java.nio.charset.StandardCharsets;
    import java.util.Base64;
    
    public class Base64Demo {
        public static void main(String[] args) {
            PrintStream out = new PrintStream(System.out, true, StandardCharsets.UTF_8);
            String text = "Hello, தமிழ் 👋!";
            byte[] data = text.getBytes(StandardCharsets.UTF_8);
    
            out.println(Base64.getEncoder().encodeToString(data));
    
            // URL-safe: - and _ instead of + and /, here without = padding.
            String urlSafe = Base64.getUrlEncoder().withoutPadding().encodeToString(data);
            out.println(urlSafe);
    
            // Java's decoders accept missing padding.
            out.println(new String(Base64.getUrlDecoder().decode(urlSafe), StandardCharsets.UTF_8));
    
            // The MIME decoder skips line breaks (and, careful, any other character
            // outside the alphabet). getMimeEncoder() writes 76-character lines with CRLF.
            out.println(new String(Base64.getMimeDecoder().decode("SGVsbG8s\nIHdvcmxk"), StandardCharsets.UTF_8));
    
            // The basic decoder rejects anything outside the alphabet.
            try {
                Base64.getDecoder().decode("SGVs!G8=");
            } catch (IllegalArgumentException e) {
                out.println("error: not valid Base64");
            }
        }
    }

    Standard library only (java.util.Base64, Java 8 or later; the PrintStream charset overload needs Java 10).

    JavaScript

    // Base64 in JavaScript (browsers and Node.js), standard library only.
    const text = 'Hello, தமிழ் 👋!';
    
    // btoa() only accepts Latin-1 characters, so turn the text into UTF-8 bytes first.
    const bytes = new TextEncoder().encode(text);
    let binary = '';
    for (const b of bytes) binary += String.fromCharCode(b);
    const b64 = btoa(binary);
    console.log(b64);
    
    // URL-safe: - and _ instead of + and /, usually without = padding.
    const urlSafe = b64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
    console.log(urlSafe);
    
    // Decode: map back to the standard alphabet; atob() accepts missing padding.
    const fromBase64 = (s) => new TextDecoder().decode(Uint8Array.from(atob(s), (c) => c.charCodeAt(0)));
    console.log(fromBase64(urlSafe.replace(/-/g, '+').replace(/_/g, '/')));
    
    // atob() skips spaces and line breaks, so MIME and PEM bodies decode as they are.
    console.log(fromBase64('SGVsbG8s\nIHdvcmxk'));
    
    // Anything outside the alphabet throws.
    try {
      atob('SGVs!G8=');
    } catch {
      console.log('error: not valid Base64');
    }
    
    // Newer browsers also have Uint8Array.prototype.toBase64() and Uint8Array.fromBase64(),
    // with { alphabet: 'base64url', omitPadding: true } options. Check support first.

    Python

    # Base64 in Python, standard library only.
    import base64
    import binascii
    
    text = "Hello, தமிழ் 👋!"
    data = text.encode("utf-8")
    
    print(base64.b64encode(data).decode("ascii"))
    
    # URL-safe: - and _ instead of + and /. Strip the = padding if you do not want it.
    url_safe = base64.urlsafe_b64encode(data).decode("ascii").rstrip("=")
    print(url_safe)
    
    # urlsafe_b64decode needs the padding back.
    padded = url_safe + "=" * (-len(url_safe) % 4)
    print(base64.urlsafe_b64decode(padded).decode("utf-8"))
    
    # Without validate=True, b64decode drops every character outside the alphabet,
    # including line breaks (fine for MIME and PEM) but also real mistakes.
    print(base64.b64decode("SGVsbG8s\nIHdvcmxk").decode("utf-8"))
    
    # validate=True rejects anything outside the alphabet instead.
    try:
        base64.b64decode("SGVs!G8=", validate=True)
    except binascii.Error:
        print("error: not valid Base64")

    Go

    // Base64 in Go, standard library only.
    package main
    
    import (
    	"encoding/base64"
    	"fmt"
    )
    
    func main() {
    	text := "Hello, தமிழ் 👋!"
    	data := []byte(text) // Go strings are already UTF-8
    
    	fmt.Println(base64.StdEncoding.EncodeToString(data))
    
    	// RawURLEncoding: - and _ instead of + and /, no = padding.
    	urlSafe := base64.RawURLEncoding.EncodeToString(data)
    	fmt.Println(urlSafe)
    
    	decoded, err := base64.RawURLEncoding.DecodeString(urlSafe)
    	if err != nil {
    		panic(err)
    	}
    	fmt.Println(string(decoded))
    
    	// The decoders skip \r and \n, so MIME and PEM bodies decode as they are.
    	mime, err := base64.StdEncoding.DecodeString("SGVsbG8s\nIHdvcmxk")
    	if err != nil {
    		panic(err)
    	}
    	fmt.Println(string(mime))
    
    	// Anything else outside the alphabet is an error (a CorruptInputError with its offset).
    	if _, err := base64.StdEncoding.DecodeString("SGVs!G8="); err != nil {
    		fmt.Println("error: not valid Base64")
    	}
    }

    Last verified . Each example is run automatically and its output checked.

    Settings

    Deletes everything this site keeps in your browser: saved inputs, favorites, theme, accent color and tool options.