#### 2023-04-13 # How to Get Base64 of a File or String and Decode It Base64 encoding is a method of converting binary data into an ASCII string format by translating it into a radix-64 representation. This is particularly useful for transmitting data over media that are designed to deal with text. In this blog post, we will explore how to encode and decode Base64 for files and strings using various methods across different platforms and programming languages. ## 1. From the Terminal ### macOS To encode a file or string to Base64 in macOS, you can use the `base64` command in the terminal. **Encoding a File:** ```bash base64 /path/to/your/file ``` **Encoding a String:** ```bash echo -n "Your string here" | base64 ``` **Decoding a File:** ```bash base64 -d /path/to/your/file.b64 ``` **Decoding a String:** ```bash echo -n "YourBase64String" | base64 -d ``` #### Encoding a file ```shell cat /path/to/your/file.txt | base64 ``` ### Linux On Linux, you can also use the `base64` command. **Encoding a File:** ```bash base64 /path/to/your/file ``` **Encoding a String:** ```bash echo -n "Your string here" | base64 ``` **Decoding a File:** ```bash base64 -d /path/to/your/file.b64 ``` **Decoding a String:** ```bash echo -n "YourBase64String" | base64 -d ``` ## 2. Using JavaScript ### In the Browser You can use the `btoa` and `atob` functions to encode and decode Base64 in the browser. **Encoding a String:** ```javascript const encodedString = btoa("Your string here"); ``` **Decoding a String:** ```javascript const decodedString = atob("YourBase64String"); ``` ### Using Node.js In Node.js, you can use the Buffer class. **Encoding a String:** ```javascript const encodedString = Buffer.from("Your string here").toString('base64'); ``` **Decoding a String:** ```javascript const decodedString = Buffer.from("YourBase64String", 'base64').toString('utf-8'); ``` ### Using Bun Bun also supports Buffer similar to Node.js. **Encoding a String:** ```javascript const encodedString = Bun.buffer("Your string here").toString('base64'); ``` **Decoding a String:** ```javascript const decodedString = Bun.buffer("YourBase64String", 'base64').toString('utf-8'); ``` ### Using Deno In Deno, you can use the `btoa` and `atob` functions as well. **Encoding a String:** ```javascript const encodedString = btoa("Your string here"); ``` **Decoding a String:** ```javascript const decodedString = atob("YourBase64String"); ``` ## 3. Using Python In Python, you can use the built-in `base64` module. **Encoding a String:** ```python import base64 encoded_string = base64.b64encode(b"Your string here").decode('utf-8') ``` **Decoding a String:** ```python decoded_string = base64.b64decode("YourBase64String").decode('utf-8') ``` **Encoding a File:** ```python with open("path/to/your/file", "rb") as file: encoded_string = base64.b64encode(file.read()).decode('utf-8') ``` **Decoding a File:** ```python with open("path/to/your/file.b64", "wb") as file: file.write(base64.b64decode("YourBase64String")) ```