Blog · Base64 How-To

How to Encode Text to Base64: The Complete Guide

Turn any text string into Base64. Step-by-step methods for browser, JavaScript, Python, and online tools. UTF-8, emojis, multi-line text โ€” all covered.

You have a text string and you need it in Base64. Maybe you are building a Basic Auth header, encoding credentials for an API call, or embedding text in a data URI. Whatever the reason, encoding text to Base64 is one of those operations that comes up constantly in web development. This guide covers every method, from zero-setup online tools to production-ready code, including UTF-8 safe encoding for non-English text and emojis.

In short: Encoding text to Base64 converts any string into a safe ASCII format using btoa() in JavaScript, Python's base64.b64encode(), or the base64 command. Always encode text as UTF-8 bytes first to support emojis and non-English characters.

The Fastest Way: Online Text to Base64 Tool

If you need a result right now, use our free text to Base64 encoder:

  1. Type or paste your text on base64go.com
  2. Switch the mode to Encode
  3. The Base64 output appears instantly in the right panel
  4. Click Copy to grab the encoded string

No page refresh, no server round-trip. Encoding happens locally with JavaScript, so your text never leaves your computer. If you need to go the other direction, see our Base64 decoding guide.

JavaScript: The Built-in Way

JavaScript gives you two functions: btoa() ("binary to ASCII") for encoding and atob() ("ASCII to binary") for decoding. For a deeper understanding of how these functions work, read our Base64 translator guide which explains the underlying algorithm.

const text = "Hello, World!";
const encoded = btoa(text);
console.log(encoded); // "SGVsbG8sIFdvcmxkIQ=="
Gotcha with non-Latin text: btoa() throws an error on characters outside the Latin-1 range (code points above 255). This includes emojis, Chinese characters, and most non-English scripts. Use the UTF-8 safe method below instead.
function toBase64UTF8(str) {
  const bytes = new TextEncoder().encode(str);
  const binString = Array.from(bytes, b => String.fromCodePoint(b)).join('');
  return btoa(binString);
}

toBase64UTF8("Hello ๐Ÿ˜€"); // Works with emojis
toBase64UTF8("ใ“ใ‚“ใซใกใฏ"); // Works with Japanese

Python: One Import Does It

Python's base64 module handles everything, including multi-byte characters:

import base64

# Simple ASCII text
text = "Hello, World!"
encoded = base64.b64encode(text.encode("utf-8")).decode("utf-8")
print(encoded) # SGVsbG8sIFdvcmxkIQ==

# Any Unicode, including emojis and non-Latin scripts
text_unicode = "Cafรฉ rรฉsumรฉ ๐ŸŒ"
encoded_unicode = base64.b64encode(text_unicode.encode("utf-8")).decode("utf-8")
print(encoded_unicode) # Q2Fmw6kgcsOpc3Vtw6kg8J+MjQ==

Command Line: No Programming Required

# Linux / WSL
echo -n "Hello, World!" | base64
# Output: SGVsbG8sIFdvcmxkIQ==

# macOS (different flag)
echo -n "Hello, World!" | base64 -i

# Windows PowerShell
[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("Hello, World!"))

# Encode an entire text file
base64 input.txt > encoded.b64
Watch the newline: The echo command appends a newline by default. Always use echo -n when encoding text, or the newline gets encoded too.

Encoding Methods Comparison

MethodUTF-8 SafeSetupBest For
Online Toolโœ“NoneQuick one-off conversions
JS btoa()โœ—NoneSimple ASCII text in browsers
JS + TextEncoderโœ“NoneAny Unicode text in browsers
Python base64โœ“Python installedScripts, automation, any text
CLI base64โœ“Unix/macOSShell pipelines, file encoding

Real-World Uses for Text-to-Base64

  • HTTP Basic Authentication. The Authorization: Basic ... header is btoa("username:password"). Note: this is encoding, not security โ€” use HTTPS to protect the header in transit. Learn why Base64 is not encryption.
  • Storing credentials in CI/CD. GitHub Actions and similar platforms accept Base64-encoded secrets. Encode your config, paste it, and decode it in your pipeline.
  • Embedding JSON in URLs. A Base64URL-encoded JSON payload in a query parameter survives URL encoding without corruption.
  • Data URIs for text content. data:text/plain;base64,SGVsbG8= renders as "Hello" in the browser. For image Data URIs, see our Base64 pictures guide.

Decoding Back: The Round Trip

Any text encoded to Base64 can be decoded back to the original, as long as you know the original encoding (UTF-8 is the universal default). Here is the round trip:

const original = "Hello, World!";
const encoded = btoa(original);
const decoded = atob(encoded);
console.log(original === decoded); // true

For UTF-8 safe round trips, pair TextEncoder with a decoder that uses TextDecoder on the way back. See our Base64 decoding guide for the full decode function.

Frequently Asked Questions

Can I encode any text to Base64, including emojis?

Yes. Any Unicode text โ€” including emojis, Chinese, Arabic, Cyrillic โ€” can be Base64-encoded. You must encode the text as UTF-8 bytes first, then Base64-encode those bytes. Our online tool handles this automatically. In code, use TextEncoder in JavaScript or .encode("utf-8") in Python.

Is Base64 encoding reversible?

Yes, completely. Base64 is a deterministic, lossless encoding. Any properly encoded text can be decoded back to the exact original using the standard Base64 algorithm defined in RFC 4648.

Does Base64 encoding increase the text size?

Yes. Base64 expands data by about 33%. Every 3 bytes of input produce 4 Base64 characters. For a short text string this is negligible, but for large files, the size increase is noticeable.

Can I use Base64 to hide or encrypt passwords?

No. Base64 is encoding, not encryption. A Base64-encoded password can be decoded instantly by anyone โ€” it provides zero security. For passwords, use proper hashing like bcrypt or Argon2.

Encode text to Base64 now. Fast and private.

Type or paste your text and get the Base64 output instantly.

Encode Text Now