Encode and decode base 64 strings with Javascript, Typescript, and Php
One of the most common tasks when manipulating strings is converting to base 64. This kind of text encoding uses only printable standard characters to represent the information (like letters and numbers) so it can be transferred without errors across multiple environments. It is also a valuable format to send binary data with a set of safe characters and is commonly used to append information to URLs. The main disadvantage of this format is that it increases the final size of the data approximately by 33%, so we may consider this depending on what we want to do.
You can encode to base 64 online here
You can decode from base 64 online here
Encoding/decoding Base 64 strings with the TurboCommons library
Using the TurboCommons library this process is as simple as calling a method and getting the result. There's two general-purpose methods stringToBase64() and base64ToString() inside the ConversionUtils class which let you perform the base 64 encoding and decoding. Let's see an easy example for both methods on different programming languages:
Encode and decode Base 64 in Php
Download the latest TurboCommons phar file from the downloads section, place it on your project as a dependency and run the following code:
require '%path-to-your-project-dependencies-folder%/turbocommons-php-X.X.X.phar';
use org\turbocommons\src\main\php\utils\ConversionUtils;
echo ConversionUtils::stringToBase64('hello')."<br>";
echo ConversionUtils::base64ToString('aGVsbG8=')."<br>";
The output of the previous code should be:
aGVsbG8=
hello
Encode and decode Base 64 in Javascript on a website
Download the latest turbocommons-es5.js file from the downloads section or use npm to add the dependency to your project (npm install turbocommons-es5). Then run the following code:
<script src="turbocommons-es5/turbocommons-es5.js"></script>
<script>
var ConversionUtils = org_turbocommons.ConversionUtils;
console.log(ConversionUtils.stringToBase64('hello'));
console.log(ConversionUtils.base64ToString('aGVsbG8='));
</script>
The console log for the previous code should be:
aGVsbG8=
hello
Encode and decode Base 64 in Typescript (TS)
The recommended way is to use npm to obtain the turbocommons dependency by executing the following command at the root of your project:
npm install turbocommons-ts
Or you can download the latest turbocommons-ts files from the downloads section and copy the dependency by yourself. Then run the following code:
import { ConversionUtils } from 'turbocommons-ts';
console.log(ConversionUtils.stringToBase64('hello'));
console.log(ConversionUtils.base64ToString('aGVsbG8='));
The output of the previous code should be:
aGVsbG8=
hello
Encode and decode Base 64 in a NodeJs App
Install the dependency by executing the following command at the root of your project:
npm install turbocommons-ts
And then run the following code:
const {ConversionUtils} = require('turbocommons-ts');
console.log(ConversionUtils.stringToBase64('hello'));
console.log(ConversionUtils.base64ToString('aGVsbG8='));
Which should output:
aGVsbG8=
hello