Encoding - decoding Base64 on Windows, Linux and Mac
When you urgently need to encode file to base64
Here are examples how to Encode and Decode file to Base64 on Windows, Linux and Mac.
Convert file to base64 on Windows
To convert an ordinary file to Base64 on Windows, one can use the following methods:
Using Command Prompt with Certutil
-
Open Command Prompt.
-
Run the command:
certutil -encode
Replace
with your file's path and
with the desired output file name (e.g.,encoded.b64
). -
To display the Base64 content directly on the screen without saving to a temporary file:
certutil -encode tmp.b64 && findstr /v /c:- tmp.b64 && del tmp.b64
This avoids leaving temporary files by deleting
tmp.b64
after processing.
Using PowerShell
- Open PowerShell.
- Run the command:
Replace
[Convert]::ToBase64String([System.IO.File]::ReadAllBytes("your_file_path"))
"your_file_path"
with the full path to your file. This will output the Base64 string directly in the console.
Using a Custom Script
If you need more flexibility, you can use a script like ConvertTo-Base64.ps1
. It allows encoding files to Base64 and optionally saving the output to a file or formatting it as JSON.
These methods enable quick and efficient Base64 encoding without requiring additional software installations.
Convert file to base64 on Linux
To convert an ordinary file to Base64 on Linux, you can use the base64
command,
which is typically pre-installed on most Linux distributions.
This one is so much better compared to Windows! :)
Here’s how to do it:
Basic Syntax
base64 [OPTIONS] [FILE]
Steps to Encode a File
-
Open a terminal.
-
Run the following command:
base64 input_file > output_file
Replace
input_file
with the path to your file andoutput_file
with the name of the file where you want to save the Base64-encoded content.Example:
base64 myfile.txt > encoded.txt
-
If you want to display the encoded content directly in the terminal:
base64 input_file
Optional Flags
-w
: Wrap lines after a specified number of characters (default is 76). Use-w 0
for no line wrapping. Example:base64 -w 0 input_file > output_file
Decode a Base64 File
To decode a Base64-encoded file back to its original format:
base64 --decode encoded_file > decoded_file
This method is efficient and works on most Linux distributions without requiring additional installations.
Encode / Decode base64 on Mac
This one is close to Linux but differs a bit.
To convert an ordinary file to Base64 on macOS, you can use the built-in base64
command. Here’s how:
Steps to Encode a File
-
Open the Terminal.
-
Run the following command:
base64 -i input_file > output_file
Replace
input_file
with the path to your file andoutput_file
with the name of the file where you want to save the Base64-encoded content.Example:
base64 -i myfile.txt > encoded.txt
-
If you want to display the Base64-encoded content directly in the terminal:
base64 -i input_file
Optional Clipboard Integration
You can encode a file and copy the result directly to your clipboard using:
base64 -i input_file | pbcopy
Decoding a Base64 File
To decode a Base64-encoded file back to its original format:
base64 -D -i encoded_file > decoded_file
These commands are efficient and work on all modern macOS versions starting from 10.7