Base64Doc

Command-Line Base64 Compared: Linux, macOS & PowerShell

Scripts written on Ubuntu frequently break on macOS laptops and Windows CI runners because base64 CLI flags diverge sharply across GNU coreutils, Apple BSD, and PowerShell.

GNU Coreutils vs Apple BSD vs PowerShell UTF-16LE Pitfalls

Prevent cross-OS CI/CD script crashes caused by GNU -w 0 vs macOS BSD -D vs PowerShell’s default UTF-16LE file redirection.

Read full technical details

A classic cross-platform break hits engineering teams where Ubuntu Docker containers run GNU coreutils base64 while developers test locally on macOS BSD base64. On Linux (GNU), encoding wraps lines at 76 characters unless -w 0 is passed, and decoding uses lowercase -d. On macOS (BSD), encoding emits a single unwrapped line automatically, passing -w 0 fails immediately with "invalid option -- w", and older macOS scripts require uppercase -D.

Windows PowerShell introduces a subtler data-corruption trap: piping or redirecting text with > or Out-File defaults to UTF-16LE encoding in Windows PowerShell 5.1, inserting 0x00 null bytes after every ASCII character and doubling Base64 file size or mangling decoded binary files.

Using explicit [IO.File]::WriteAllBytes() and [Convert]::FromBase64String() in PowerShell—and remembering that `openssl base64 -d -A` requires `-A` for unwrapped single-line payloads past 64 characters—keeps automation scripts reliable across every runner.

Sanitizing API Wrappers Before Piping Into Terminal Decoders

Strip Data URI prefixes, JSON \/ slashes, and URL-Safe -/_ characters ahead of base64 -d so shell pipes do not abort on real API responses.

Read full technical details

Neither GNU base64, macOS BSD base64, nor .NET [Convert]::FromBase64String handles data:application/pdf;base64, headers, JSON-escaped \/ slashes, or unpadded RFC 4648 §5 Base64URL strings natively. Piping raw webhook JSON directly into terminal decoders fails instantly.

Normalize wrappers first with sed/tr—or paste the raw payload into Base64Doc to unpack multi-attachment JSON automatically and copy OS-calibrated Bash or PowerShell snippets tailored to your exact payload.

Verifying Terminal Output With Magic Bytes & Zero-Error Exit Checks

Inspect decoded binary headers using file or xxd right after shell decoding so HTML error bodies never get saved as fake PDFs.

Read full technical details

Shell pipelines inside CI jobs frequently ignore exit codes or write 502 Bad Gateway HTML text straight into report.pdf when an API endpoint fails. Follow every base64 -d command with `head -c 5 out.pdf` (checking for `%PDF-`) or `file -b out.bin`, and enable `set -euo pipefail` in Bash so invalid Base64 characters fail the build step immediately rather than shipping silent blank artifacts.

When switching between macOS laptops, Alpine/Ubuntu containers, and Windows GitHub Actions runners, testing suspect strings inside Base64Doc’s browser workbench provides identical cross-platform diagnostics and ready-to-paste snippets.

Production Reference Code

# Linux (GNU coreutils): wrap disabled (-w 0), decode (-d)
base64 -w 0 invoice.pdf > invoice.b64 && base64 -d invoice.b64 > out.pdf

# macOS (BSD): uppercase -D to decode, single line by default (no -w 0)
base64 -i invoice.pdf -o invoice.b64 && base64 -D -i invoice.b64 -o out.pdf

# Windows PowerShell: explicit UTF-8 + WriteAllBytes (avoids UTF-16LE > redirection trap)
[IO.File]::WriteAllBytes("out.pdf", [Convert]::FromBase64String((Get-Content -Raw invoice.b64).Trim()))

Step-by-Step Local Workflow

STEP 1

Match Flags to Target OS

Use -w 0 and -d on GNU/Linux; omit -w and use -D / -i / -o on macOS BSD; pass -A for single-line OpenSSL input.

STEP 2

Avoid PowerShell Redirection >

Use [Text.Encoding]::UTF8.GetBytes() and [IO.File]::WriteAllBytes() in PowerShell instead of UTF-16LE shell redirection.

STEP 3

Pre-Clean Data URIs & URL-Safe Chars

Strip data:*;base64, prefixes, unescape \/, and translate -_ to +/ before calling terminal decoders.

STEP 4

Verify Decoded Header Bytes

Run file out.bin (or check %PDF- / PK\x03\x04) to confirm output is genuine binary rather than an HTML error response.

Test Directly in Browser Workbench

Frequently Asked Questions

Why does base64 -w 0 fail on macOS?

Yes, macOS ships BSD base64 which lacks a -w flag and already emits unwrapped single-line output by default; remove -w 0 on macOS scripts.

What is the cleanest PowerShell one-liner to decode Base64 into a PDF file?

Use [IO.File]::WriteAllBytes("out.pdf", [Convert]::FromBase64String($b64.Trim())) in PowerShell so binary %PDF- octets write straight to disk without UTF-16LE or newline corruption.

Does GNU base64 -d handle Base64URL strings with - and _?

No, translate characters with tr "-_" "+/" and restore missing = padding first, or paste directly into Base64Doc Base64URL Decode.

Why did openssl base64 -d truncate or ignore my long single-line Base64 string?

Without the -A flag, OpenSSL expects 64-character PEM line breaks and silently stops decoding long unwrapped lines; pass `openssl base64 -d -A`.

Why did PowerShell [Convert]::ToBase64String produce twice as many bytes for plain text?

[Text.Encoding]::Unicode encodes UTF-16LE (2 bytes per ASCII character) rather than 1-byte UTF-8. Switch to [Text.Encoding]::UTF8.GetBytes($text) for cross-platform UTF-8 compatibility.

Explore All 34 Base64 Converters & Developer Tools