Base64 Encode and Decode on Windows, Mac, and Linux
Copy practical Base64 commands for PowerShell, macOS, and Linux. Learn how to handle UTF-8 text, files, padding, and unexpected newlines.
Choose text decoding or file decoding
Base64 represents bytes using printable characters. Those bytes might describe a sentence, an image, a PDF, or compressed data. Successfully decoding Base64 does not mean the result is readable text.
The DevX Tool decoder interprets decoded bytes as UTF-8 text. For example, SGVsbG8= becomes Hello. A binary image may produce an invalid UTF-8 error even when its Base64 is valid. Use the file commands below for that case. Encoding is reversible and does not protect secrets.
Windows: encode and decode with PowerShell
Convert the text to UTF-8 bytes before encoding. When decoding text, reverse those two operations. This preserves characters from international alphabets and emoji, unlike treating each character as a single byte.
$text = "Hello"
$bytes = [System.Text.Encoding]::UTF8.GetBytes($text)
[Convert]::ToBase64String($bytes)
# SGVsbG8=
$decoded = [Convert]::FromBase64String("SGVsbG8=")
[System.Text.Encoding]::UTF8.GetString($decoded)
# HellomacOS and Linux: use printf instead of echo
The following commands assume a POSIX-style shell such as bash or zsh. printf sends exactly the five bytes in Hello. A plain echo command usually adds a newline, which changes the Base64 result. GNU base64 on Linux accepts --decode, while macOS provides -D.
Both commands produce Hello without adding a display newline, so your shell prompt may appear immediately after the output. That is expected. Check the local command’s help or manual when using another Unix implementation.
# Encode on macOS or Linux
printf '%s' 'Hello' | base64
# SGVsbG8=
# Decode on Linux (GNU coreutils)
printf '%s' 'SGVsbG8=' | base64 --decode
# Decode on macOS
printf '%s' 'SGVsbG8=' | base64 -DDecode an encoded file to its original bytes
These examples assume encoded.txt contains only Base64 data. If you copied a data URL, remove the prefix through the first comma, such as data:application/pdf;base64,. Choose a new output path because redirection and WriteAllBytes can replace an existing file.
Do not open or execute an unknown decoded file just because decoding succeeded. Base64 describes the representation of the data, not its origin or trustworthiness.
# Linux
base64 --decode encoded.txt > decoded.bin
# macOS
base64 -D -i encoded.txt -o decoded.bin
# Windows PowerShell (run in the folder containing encoded.txt)
$encoded = Get-Content -Raw -LiteralPath ./encoded.txt
$bytes = [Convert]::FromBase64String($encoded)
$outputPath = Join-Path (Get-Location).Path "decoded.bin"
[System.IO.File]::WriteAllBytes($outputPath, $bytes)Troubleshoot padding and mismatched output
Standard Base64 uses + and /; Base64url uses - and _. Some Base64url formats omit the trailing = padding. Use a decoder that explicitly supports the format you received rather than repeatedly adding padding to damaged input.
GNU base64 wraps long encoded output by default. Its --wrap=0 option disables wrapping when a protocol requires one line. A trailing newline in the original data is different from line wrapping in the encoded representation: removing the former changes the decoded bytes.
If a copied value contains spaces where + used to be, it may have passed through a form query parser. Recover the original value and encode it as a query parameter correctly; blindly removing spaces cannot reliably recover lost data.
Frequently asked questions
Why does Hello encode differently with echo?
Hello is SGVsbG8=. Hello followed by a newline is SGVsbG8K. The final newline is an extra input byte, so the encoded strings differ.
Can Base64 be decrypted without a key?
Base64 is decoded, not decrypted. It has no secret key. Anyone with the string can recover its bytes.
References
Check the official documentation for command options and runtime-specific behavior.
Integrated Tools & Learning Resources
DevX Tool combines deep engineering tutorials with client-side interactive tooling so you can test code, verify outputs, and troubleshoot bugs right away.
Related tutorials and articles
Base64 Data URLs vs Blob URLs: Size and Performance
The Hidden 33% Network Overhead: When to Use Base64 Data URLs vs Blob URLs and CDN Assets
Read article Web Standards & DebuggingFix the btoa Latin1 Error with UTF-8 Encoding
Resolving "The string to be encoded contains characters outside of the Latin1 range" (DOMException: Failed to execute 'btoa')
Read article Web troubleshootingHTTP Error Codes: 400, 401, 403, 404, 429 & 500
Find what common HTTP errors mean, what to check in an API response, and when a retry can help. Includes a status-code comparison and debugging steps.
Read article Developer fundamentalsJSON vs YAML vs TOML: Which Configuration Format Fits?
Compare JSON, YAML, and TOML using the same configuration example. Understand comments, indentation, data types, and safe conversion between formats.
Read article