- 16 November 2024
- Reading time: 3 minutes
- Victor Domingos
- Tutorials
I recently stumbled upon a situation where I needed to quickly convert a Base64 string coming from a web api and save it into a file in disk in order to inspect its contents. Sure, I could have spun up a small program in C# or Python. But honestly, that felt like an overkill for what was essentially a one-off task. In the past I had used an online Base64 converter for a similar task, so it crossed my mind, but sharing potentially sensitive data with unknown third-party services didn’t feel right. So, I searched for an alternative. Surely, there had to be an simpler, more direct solution.
And it turns out, there is! This kind of task can be handled seamlessly using built-in tools like PowerShell or macOS/Linux shell, without the need for a full-fledged mini-program or compromising security. So I though it would be a good idea to share it here both with you and with my future self.
Why Base64 conversion matters
If you haven’t heard about it before, here’s a short summary. Base64 encoding is commonly used to transmit binary data (like files) in text-based formats, especially in APIs. This encoding makes data easier to handle in text-based systems, like a JSON property in a Web API response, but the result — a seemingly random characters string — is not human-readable unless decoded back to its original binary form.
So, the decoding of a Base64 string is a pretty common task in the programming world, especially when dealing with web apis.
How to do it in code
In C#, you can integrate the conversion in your program doing something along these lines:
1 2 |
|
In Python, a roughly equivalent code would be something like:
1 2 |
|
Unless you are adding these into your existing app, you will need to add the usual cerimony code surrounding those lines. So, to create your own Base64 converter, you would need some usings
, input instructions, some exception handling, and so on.
Using the console allows for a quick solution that does not require writing, saving and running another program.
How to do it in Windows PowerShell
If you’re on Windows, PowerShell is a great way to handle Base64 decoding. I am not very fond of the syntax and verbosity of its commands, but it works. The following shell command will take a Base64 you have just copied to the system clipboard and write a file at C:\path-to-folder\example_filename.pdf
:
1 |
|
How to do it in macOS, Linux and other systems
On Unix-based systems, you can generally leverage the base64
utility, which is pre-installed in most environments. Here’s an example for macOS:
macOS:
1 |
|
In other operating systems, like Linux or Haiku, you would need to replace the pbpaste
with an equivalent command that pastes the clipboard content as a stream into the base64
utility.
Linux:
1 |
|
In more recent distributions, you should be able to use the Wayland equivalent:
1 |
|
Haiku:
1 |
|
These methods work out of the box, with no need to install anything, which is nice.
Oh, and by now, you’ve probably already guessed that the CLI utilities mentioned can also work the other way around. With a little bit of work, you could use the shell to generate a Base64 string in memory from the contents of a file, and then paste it into a Postman request or any other application you’re using.