How to Encode and Decode Base64 Strings in Node.js

Base64 encoding is used to convert binary data into a text format. This is useful for transferring data over the internet, encoding binary files, storing data in text formats, etc.

In this article, we will understand what Base64 encoding is and see how we can encode binary data along with decoding in Node.js.

Why We Need Base64 Encoding?

As a JavaScript developer, we generally use JSON and XML for sending data but these formats can only work with text, not with binary data. Here comes the Base64 encoding to convert the binary data into a text format.

This allows us to embed binary data like images or files directly into text documents and also ensures that data is not altered during transmission.

How Base64 Encoding Works?

Let’s now try to understand how Base64 encoding works and how we can actually convert a text into a base64-encoded string:

Step 1: Firstly we have to convert each character of a given text into ASCII value. The ASCII value is a number that represents each character.

Step 2: Then we have to again further convert ASCII values into 8-bit binary. This means we are representing each number in the combination of 1s and 0s and it is of 8 digits.

Step 3: Since Base64 encoding works with 6-bit chunks, we have to split our 8-bit binary into 6 smaller pieces.

Step 4: Then we have to convert each 6-bit binary segment into a decimal value.

Step 5: Finally, we have to use the Base64 table to find the character that corresponds to each decimal value and create the final Base64 encoded string.

Encoding a String to Base64 Using Node.js

Node.js has a built-in buffer module for handling binary data, so we don’t have to do all these steps manually. We can use the Buffer class from this module with the toString() method and generate our Base64 encoded string.

Click here to learn more about buffers in Node.js!

Approach:

First, import the Buffer class from the buffer module. Then, use Buffer.from() to create a buffer from the input string. Finally, encode the buffer into a Base64 string using toString(‘base64’). The base64 parameter specifies that we need to convert binary data into a Base64-encoded string. That’s it.

Example 1:

Let’s take a simple string “hey” and convert it to Base64.

const { Buffer } = require('buffer');

const inputSring = 'Hey';
const buffer = Buffer.from(inputSring);
const base64String = buffer.toString('base64');

console.log(base64String);

Output:

SGV5

Example 2:

Let us take a string of longer length this time. Also, we will write the same code in just 2 lines!

const { Buffer }  = require('buffer');
console.log(Buffer.from("I am Groot").toString('base64'))

Output:

SSBhbSBHcm9vdA==

The “=” in the resultant base64 string is called padding. This is used when there are not enough bits to fill the last 6-bit block.

Decoding Base64 to Original String

To decode the Base64 encoded string to its original form, we can reverse the above process using the same buffer module.

Approach:

First, import the Buffer class from the buffer module. Then, use Buffer.from(‘base64’) to convert the Base64 string to a buffer. Then, use toString(‘utf-8’) to convert the buffer back to the decoded string.

Example 1:

Taking the output from the previous example, let’s try to get the original string.

const { Buffer } = require('buffer');

const base64String = 'SGV5';
const buffer = Buffer.from(base64String, 'base64');
const originalString = buffer.toString('utf-8');

console.log(originalString);

Output:

Hey

Example 2:

const { Buffer } = require('buffer');

const encodedString = Buffer.from("I am Groot").toString('base64');
console.log(encodedString);

console.log(Buffer.from(encodedString, 'base64').toString('utf-8'));

Output:

SSBhbSBHcm9vdA==
I am Groot

Conclusion

In short, to convert an input string into Base64, we can use Buffer.from(originalString).toString(‘base64’) and to convert it back to the original form we can use Buffer.from(encodedString, ‘base64’).toString(‘utf-8’).

Base64 encoding is a very useful method of converting binary data into text, making it compatible with different technologies that can only work with text data.

JavaScript also has something similar called ArrayBuffer, which is used in web contexts for handling binary data. Click here to learn the difference between Node.js Buffer and JavaScript ArrayBuffer.

Reference

https://stackoverflow.com/questions/6182315/how-can-i-do-base64-encoding-in-node-js

Nandana Pradosh
Nandana Pradosh
Articles: 27