Base64 Encoder

Base64 Encode Text Online

Use this free base64 encoder to convert any text to base64 instantly. Base64 encode operations transform plain text, binary data, and special characters into a safe ASCII string format suitable for transmission across systems that only handle text. Whether you are embedding images in HTML, preparing API payloads, encoding authentication credentials, or storing binary content in JSON, this tool gives you accurate base64 encoded output in real time.

What is Base64 Encoding

Base64 is a binary-to-text encoding scheme that represents binary data using a set of 64 printable ASCII characters. The standard Base64 alphabet consists of the uppercase letters A through Z, lowercase letters a through z, digits 0 through 9, and two additional characters: plus (+) and forward slash (/). A padding character, the equals sign (=), is used to ensure the output length is always a multiple of four.

The encoding was originally designed to allow binary data to travel safely through text-based systems such as email (MIME) and early internet protocols that could only handle 7-bit ASCII. Before Base64 became widespread, transmitting binary files like images or executables over email would often result in data corruption because intermediate mail servers would strip or modify bytes outside the ASCII range. Base64 solved this problem by guaranteeing that every byte in the output falls within the safe printable ASCII range.

Base64 is defined in RFC 4648, which standardizes the alphabet, padding rules, and line-length conventions. The standard is intentionally simple: it does not provide encryption, compression, or error detection. Its sole purpose is to represent arbitrary binary data as ASCII text. This simplicity is what makes Base64 so universally supported. Virtually every programming language, web browser, and operating system includes built-in Base64 encoding and decoding functions.

One important characteristic of Base64 is that it increases data size by approximately 33 percent. Every three bytes of input produce four bytes of output. This overhead is the trade-off for universal text compatibility. For most use cases, this size increase is acceptable, but it is worth considering when encoding very large files or when bandwidth is constrained.

How Base64 Encoding Works

The Base64 encoding algorithm operates by processing input data in groups of three bytes (24 bits) at a time. Each 24-bit group is divided into four 6-bit segments, and each 6-bit segment is mapped to one of the 64 characters in the Base64 alphabet. This process continues until all input bytes have been consumed, with padding applied at the end if the input length is not a multiple of three.

Here is the step-by-step process. First, the encoder reads three bytes of input data, producing 24 bits. These 24 bits are split into four groups of 6 bits each. Each 6-bit value, which ranges from 0 to 63, is used as an index into the Base64 alphabet table. The character at that index becomes the next character in the output. For example, the text "Man" has the ASCII bytes 77, 97, and 110. In binary, that is 01001101 01100001 01101110. Splitting into 6-bit groups gives 010011, 010110, 000101, 101110, which map to indices 19, 22, 5, and 46, producing the characters T, W, F, and u. So "Man" encodes to "TWFu".

When the input length is not evenly divisible by three, padding is required. If one byte remains at the end, the encoder pads it with zeros to create two 6-bit groups, outputs two Base64 characters, and appends two equals signs. If two bytes remain, three 6-bit groups are produced, three Base64 characters are output, and one equals sign is appended. This padding ensures that the decoder can determine exactly how many bytes were in the original input. If you need a variant that omits padding for use in URLs, our Base64URL encoding tool provides a URL-safe alternative.

The decoding process is the exact reverse. Each Base64 character is converted back to its 6-bit value, four characters are combined into three bytes, and padding characters indicate how many trailing bytes to discard. For quick decoding of Base64 strings, our Base64 decoder tool handles the reverse transformation instantly.

Syntax Comparison

Base64 encoding is natively supported in virtually every programming language. Here is how to perform the encoding in several popular environments:

JavaScript (Browser): The built-in btoa() function encodes a string to Base64. For example, btoa("Hello World") returns "SGVsbG8gV29ybGQ=". Note that btoa() only handles Latin-1 characters. For Unicode text, you must first encode to UTF-8 bytes using TextEncoder, then convert to a binary string before calling btoa().

JavaScript (Node.js): Use Buffer.from("Hello World").toString("base64") to produce "SGVsbG8gV29ybGQ=". The Buffer class handles binary data natively and supports multiple encodings including base64, hex, and utf8.

Python: The base64 module provides the b64encode function. Import it with import base64, then call base64.b64encode(b"Hello World").decode("ascii") to get "SGVsbG8gV29ybGQ=". The input must be a bytes object, so string data needs to be encoded to bytes first using the encode() method.

Java: Since Java 8, the java.util.Base64 class provides encoding and decoding. Use Base64.getEncoder().encodeToString("Hello World".getBytes()) to produce the Base64 string. The encoder handles byte arrays directly and returns a String.

PHP: The base64_encode() function is built into the language. Calling base64_encode("Hello World") returns "SGVsbG8gV29ybGQ=". PHP also provides base64_decode() for the reverse operation.

Command Line (Linux/macOS): Use echo -n "Hello World" | base64 to encode text from the terminal. The -n flag prevents echo from adding a trailing newline, which would otherwise be included in the encoded output and change the result.

Common Use Cases

Base64 encoding is used extensively across web development, software engineering, and data processing:

Data URIs and Inline Images: One of the most common uses of Base64 on the web is embedding images directly in HTML or CSS using data URIs. Instead of referencing an external image file, you can encode the image bytes as Base64 and include them inline: <img src="data:image/png;base64,iVBOR...">. This eliminates an extra HTTP request and can improve page load performance for small images. For converting image files to Base64 data URIs, our image to Base64 converter streamlines the process.

API Authentication: HTTP Basic Authentication requires the client to send credentials in the format username:password encoded as Base64 in the Authorization header. For example, the credentials "admin:secret" would be sent as Authorization: Basic YWRtaW46c2VjcmV0. While Base64 is not encryption and provides no security on its own, it ensures that special characters in passwords do not break the HTTP header format.

Email Attachments (MIME): The MIME standard uses Base64 to encode binary attachments in email messages. Since email was originally designed for 7-bit ASCII text, binary files like PDFs, images, and archives must be Base64 encoded before being included in an email body. The Content-Transfer-Encoding header is set to "base64" to indicate this encoding.

JSON Web Tokens: JWTs use a URL-safe variant of Base64 (Base64URL) to encode the header and payload sections. Each section is a JSON object that is Base64URL encoded and then concatenated with periods. If you work with JWTs regularly, our JWT token decoder tool can parse and display the contents of any token.

Storing Binary Data in Text Formats: When you need to include binary data in XML, JSON, YAML, or other text-based formats, Base64 encoding is the standard approach. Database systems sometimes store binary blobs as Base64 text in text columns, and configuration files may include encoded certificates or keys.

Encoding Special Characters for Transmission: Any time data must pass through a channel that might modify or reject non-ASCII bytes, Base64 provides a safe encoding. This includes URL query parameters, form submissions, cookie values, and inter-process communication over text-based protocols.

Base64 Encode Examples

Here are detailed examples showing how different types of input are transformed by the Base64 encoding process:

Example 1 - Simple Text: The string "Hello, World!" encodes to "SGVsbG8sIFdvcmxkIQ==". The input is 13 bytes, which is not a multiple of three, so two padding characters are appended. The comma, space, and exclamation mark are all handled correctly because they fall within the ASCII range.

Example 2 - Single Character: The letter "A" (one byte, value 65) encodes to "QQ==". Since only one byte is present, the encoder pads to create two 6-bit groups and appends two equals signs. This is the maximum padding you will ever see in a Base64 string.

Example 3 - JSON Payload: A JSON string like {"user":"admin","role":"editor"} encodes to "eyJ1c2VyIjoiYWRtaW4iLCJyb2xlIjoiZWRpdG9yIn0=". This is a common pattern when embedding JSON data in URLs, cookies, or other contexts where curly braces and quotes might cause parsing issues.

Example 4 - Binary-like Content: The bytes representing a simple three-byte sequence [0xFF, 0x00, 0xAB] encode to "/wCr". Notice there is no padding because the input length is exactly three bytes, which divides evenly into one complete 24-bit group.

Example 5 - Empty String: An empty input produces an empty Base64 output. This is a valid edge case that encoders must handle correctly. No padding is added because there are no incomplete groups to pad.

Example 6 - URL Content: The URL "https://example.com/path?q=test" encodes to "aHR0cHM6Ly9leGFtcGxlLmNvbS9wYXRoP3E9dGVzdA==". Note that the Base64 output may itself contain characters like plus and slash that are not URL-safe. If you need to include Base64 data in a URL, consider using the Base64URL encoding variant instead.

Example 7 - Multiline Text: When encoding text that contains newline characters, the newlines are encoded just like any other byte. The string "line1 line2" encodes to "bGluZTEKbGluZTI=". The newline character (byte value 10) is simply included in the binary input stream and processed normally by the encoding algorithm.

Frequently Asked Questions

Is Base64 encoding the same as encryption?

No, Base64 encoding is not encryption and provides no security whatsoever. Base64 is a reversible encoding scheme that anyone can decode without a key or password. It is designed purely for data format conversion, not for protecting sensitive information. If you need to secure data, use proper encryption algorithms like AES or RSA before optionally Base64 encoding the encrypted output for text-safe transmission. Never rely on Base64 alone to protect passwords, tokens, or other sensitive data.

Why does Base64 increase the size of data?

Base64 increases data size by approximately 33 percent because it represents every three bytes of input as four bytes of output. This expansion occurs because Base64 uses only 64 characters (6 bits of information per character) to represent data that originally uses 256 possible byte values (8 bits per byte). The ratio of 8 to 6 bits means that four Base64 characters are needed to represent three original bytes, giving an expansion factor of 4/3 or about 1.33. Additional overhead may come from padding characters and, in some implementations, line breaks inserted every 76 characters.

What is the difference between Base64 and Base64URL encoding?

Standard Base64 uses the characters plus (+) and forward slash (/) in its alphabet, along with equals (=) for padding. These characters have special meanings in URLs and filenames, which can cause problems when Base64 data is used in those contexts. Base64URL replaces plus with hyphen (-) and slash with underscore (_), and typically omits padding. This makes the output safe for use in URL query parameters, URL path segments, and filenames without requiring additional percent encoding. Base64URL is defined in RFC 4648 Section 5 and is used extensively in JSON Web Tokens and other web standards.

Can I Base64 encode binary files like images and PDFs?

Yes, Base64 can encode any binary data, including images, PDFs, audio files, executables, and archives. The encoding process treats the input as a raw stream of bytes regardless of what those bytes represent. This is exactly how email attachments work: binary files are Base64 encoded for safe transmission through text-based email protocols. On the web, small images are frequently Base64 encoded and embedded directly in HTML or CSS as data URIs to reduce the number of HTTP requests.

How do I handle Unicode text when Base64 encoding?

Base64 operates on bytes, not characters. When encoding Unicode text, you must first convert the text to bytes using a character encoding like UTF-8, then Base64 encode those bytes. In JavaScript, the btoa() function only handles Latin-1 characters, so for Unicode text you need to first encode to UTF-8 using TextEncoder, convert the resulting bytes to a binary string, and then call btoa(). In Python, call string.encode("utf-8") before passing to b64encode(). Failing to properly encode Unicode text before Base64 encoding is a common source of bugs. For working with UTF-8 encoded text, our UTF-8 text encoding tool can help you understand the byte representation.

What are the padding rules in Base64?

Base64 padding ensures that the encoded output length is always a multiple of four characters. If the input byte count is divisible by three, no padding is needed. If one byte remains after processing all complete three-byte groups, two Base64 characters are output followed by two equals signs (==). If two bytes remain, three Base64 characters are output followed by one equals sign (=). The padding tells the decoder exactly how many bytes were in the final group, preventing ambiguity during decoding. Some implementations, particularly Base64URL, omit padding entirely since the decoder can infer the original length from the output length.

Is there a maximum length for Base64 encoded strings?

There is no inherent maximum length for Base64 encoded data. The encoding algorithm can process input of any size. However, practical limits exist depending on the context. Some MIME implementations insert line breaks every 76 characters as specified in RFC 2045. URL length limits (typically 2048 characters in browsers) constrain how much Base64 data can be placed in a URL. Memory and storage limits of the system processing the data also apply. For very large files, streaming Base64 encoding is recommended to avoid loading the entire file into memory at once.

How do I decode a Base64 string back to the original text?

Decoding Base64 is the exact reverse of encoding. Each Base64 character is mapped back to its 6-bit value, four characters are combined into three bytes, and padding characters indicate how many trailing bytes to discard. In JavaScript, use atob() for browser environments or Buffer.from(str, "base64") in Node.js. In Python, use base64.b64decode(). In Java, use Base64.getDecoder().decode(). You can also use our online Base64 decoder for quick conversions without writing any code.

FAQ

What is Base64 encoding?

Base64 encodes binary data into ASCII text using 64 printable characters, commonly used for data transfer.

Does it support Unicode?

Yes. This tool uses UTF-8 safe encoding to handle all Unicode characters.

Ad