Uploaded by Alex Enrique Valencia Ayola

Encoding and Decoding Base64 Strings in Node.js

advertisement
Search
Home
Sign in
Sign up
Articles
Encoding and Decoding Base64
Strings in Node.js
Scott Robinson
What is Base64 Encoding?
Base64 encoding is a way to convert data (typically
binary) into the ASCII character set. It is important to
mention here that Base64 is not an encryption or
compression technique, although it can sometimes be
confused as encryption due to the way it seems to
obscure data. In fact, size of a Base64 encoded piece of
information is 1.3333 times the actual size of your original
data.
Base64 is the most widely used base encoding technique
with Base16 and Base32 being the other two commonly
used encoding schemes.
How Does Base64 Work?
Converting data to base64 is a multistep process. Here is
how it works for strings of text:
1. Calculate the 8 bit binary version of the input text
2. Re-group the 8 bit version of the data into multiple
chunks of 6 bits
3. Find the decimal version of each of the 6 bit binary
chunk
4. Find the Base64 symbol for each of the decimal values
via a Base64 lookup table
IN THIS ARTICLE
For a better understanding of this concept, let's take a look
at an example.
Encoding Base64 Strings
with Node.js
What is Base64
Encoding?
How Does Base64
Work?
Why use Base64
Encoding?
Decoding Base64 Strings
with Node.js
Encoding Binary Data to
Base64 Strings
Decoding Base64 Strings
to Binary Data
Conclusion
Getting
Started
with AWS
in Node.js
Suppose we have string "Go win" and we want to convert
it into Base64 string. The first step is to convert this string
into binary. The binary version of "Go win" is:
01000111
01101111
00100000
01110111
01101001
01101110
You can see here that each character is represented by 8
bits. However as we said earlier, Base64 converts the data
in 8 bit binary form to chunks of 6 bits. This is because
Base64 format only has 64 characters: 26 uppercase
alphabet letters, 26 lowercase alphabet letters, 10 numeric
characters, and the "+" and "/" symbols for new line.
Base64 doesn't use all the ASCII special characters, but
only these few. Note that some implementations of
Base64 uses different special characters than "+" and "/".
Coming back to the example, let us break our 8 bit data
into chunks of 6 bits.
010001
110110
111100
100000
011101
110110
100101
You won't always be able to divide up the data in to full
sets of 6 bits, in which case you'll have to deal with
padding.
Now for each chunk above, we have to find its decimal
value. These decimal values have been given below:
Binary
010001
110110
111100
100000
011101
110110
100101
101110
Decimal
17
54
60
32
29
54
37
46
Finally we have to look the Base64 value for each of the
101110
Build the
foundation
you'll need to
provision,
deploy, and run
Node.js
applications in
the AWS cloud.
Learn Lambda,
EC2, S3, SQS,
and more!
Learn
more
decimal that we just calculated from binary data. Base64
encoding table looks like this:
Want a
remote
job?
Lead Engineer
Perk 7 days ago
Mobile Insights
Here you can see that decimal 17 corresponds to "R", and
decimal 54 corresponds to "2", and so on. Using this
encoding table we can see that the string "Go win" is
encoded as "R28gd2lu" using Base64. You can use any
online text to Base64 converter to verify this result.
Analyst China
Sensor Tower 4 days
ago
Strategy Manager
Kraken Digital
Asset Exchange 5
Why use Base64 Encoding?
days ago
Sending information in binary format can sometimes be
risky since not all applications or network systems can
handle raw binary. On the other hand, the ASCII character
set is widely known and very simple to handle for most
systems.
Crypto Markets
Associate
Kraken Digital
Asset Exchange 5
days ago
For instance email servers expect textual data, so ASCII is
typically used. Therefore, if you want to send images or
any other binary file to an email server you first need to
encode it in text-based format, preferably ASCII. This is
where Base64 encoding comes extremely handy in
converting binary data to the correct formats.
More
Jobs
Encoding Base64 Strings with
Node.js
The easiest way to encode Base64 strings in Node.js is
via the Buffer object. In Node.js, Buffer is a global object
which means that you do not need to use require
statement in order to use Buffer object in your
applications.
Internally Buffer is an immutable array of integers that is
also capable of performing many different
encodings/decodings. These include to/from UTF-8,
UCS2, Base64 or even Hex encodings. As you write code
that deals with and manipulates data, you'll likely be using
the Buffer object at some point.
Take a look at the following example. Here we will encode
a text string to Base64 using Buffer object. Save the
following code in a file "encode-text.js"
'use strict';
let data = 'stackabuse.com';
let buff = new Buffer(data);
let base64data = buff.toString('base64');
console.log('"' + data + '" converted to Base64 is "' + base64data +
In the above script we create a new buffer object and pass
it our string that we want to convert to Base64. We then
call "toString" method on the buffer object that we just
created and passed it "base64" as a parameter. The
"toString" method with "base64" as parameter will return
data in the form of Base64 string. Run the above code,
you shall see the following output.
Jobs
by HireRemote.i
Free eBook: Git
Essentials
Check out our hands-on, practical
guide to learning Git, with bestpractices, industry-accepted
standards, and included cheat
sheet. Stop Googling Git
commands and actually learn it!
Download the eBook
$ node encode-text.js
"stackabuse.com" converted to Base64 is "c3RhY2thYnVzZS5jb20="
In the output we can see Base64 counterpart for the string
that we converted to Base64.
Decoding Base64 Strings with
Node.js
Decoding Base64 string is quite similar to encoding it. You
have to create a new buffer object and pass two
parameters to its constructor. The first parameter is the
data in Base64 and second parameter is "base64". Then
you simply have to call "toString" on the buffer object but
this time the parameter passed to the method will be
"ascii" because this is the data type that you want your
Base64 data to convert to. Take a look at the following
code snippet for reference.
'use strict';
let data = 'c3RhY2thYnVzZS5jb20=';
let buff = new Buffer(data, 'base64');
let text = buff.toString('ascii');
console.log('"' + data + '" converted from Base64 to ASCII is "'
Add the data to "ascii.js" file and save it. Here we have
used "Tm8gdG8gUmFjaXNt" as the Base64 input data.
When this data is decoded it should display "No to
Racism". This is because from the last example we know
that "No to Racism" is equal to "Tm8gdG8gUmFjaXNt".
Run the above code with Node.js. It will display following
output.
Encoding Binary Data to Base64
Strings
As mentioned in the beginning of the article, the primary
purpose of Base64 encoding is to convert binary data into
textual format. Let us see an example where we will
convert an image (binary data) into a Base64 string. Take
a look at the following example.
'use strict';
const fs = require('fs');
let buff = fs.readFileSync('stack-abuse-logo.png');
let base64data = buff.toString('base64');
console.log('Image converted to base 64 is:\n\n' + base64data);
In the above code we load an image into buffer via the
readFileSync() method of the fs module. The rest of the
process is similar to creating Base64 string from a normal
ASCII string.
When you run the above code you will see following
output.
$ node encode-image.js
Image converted to Base64 is:
iVBORw0KGgoAAAANSUhEUgAAABkAAAATCAYAAABlcqYFAAAABGdBTUEAALGPC/xhBQAAACBj
Although the actual image is very small (25x19), the
output is still fairly large, partially because Base64
increases the size of the data, as we mentioned earlier.
Decoding Base64 Strings to Binary
Data
The reverse process here is very similar to how we
decode Base64 strings, as we saw in an earlier section.
The biggest difference is the output destination and how
data is written there. Let's see the example:
'use strict';
const fs = require('fs');
let data = 'iVBORw0KGgoAAAANSUhEUgAAABkAAAATCAYAAABlc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HgIl8+t0/qsfDioxmSUWGjrRFzhYi/
'bAdVYhNeINXBMwmXWCTQGG2Y+Jj+dFrfEmiMAtmeowpo9ojTvkD+A/L1UJUMmiVfkuz6WTy
'2cKMfUSm3rhD0g4E2g197fwMZ+Bgt8rNe2iP2BhL5dgfFzrx8AfECEDdx45a0AAAAASUVOR
let buff = new Buffer(data, 'base64');
fs.writeFileSync('stack-abuse-logo-out.png', buff);
console.log('Base64 image data converted to file: stack-abuse-logo-out.p
Here you can see that we start with the Base64 data
(which could've also been received from a socket or some
other communication line) and we load it in to a Buffer
object. When creating the buffer we tell it that it's in
base64 format, which allows the buffer to parse it
accordingly for internal storage.
To save the data back in its raw PNG format, we simply
pass the Buffer object to our fs.writeFileSync method
and it does the conversion for us.
Conclusion
Base64 encoding is one of the most common ways of
converting binary data into plain ASCII text. It is a very
useful format for communicating between one or more
systems that cannot easily handle binary data, like images
in HTML markup or web requests.
In Node.js the Buffer object can be used to encode and
decode Base64 strings to and from many other formats,
allowing you to easily convert data back and forth as
needed.
What do you typically use Base64 formatting for in
Node.js? Let us know in the comments!
#javascript
#node
Was this article helpful?
You might also like...
Last Updated: July 16th, 2021
•
Generating PDF Files in Node.js with PDFKit
•
Using Mocks for Testing in JavaScript with
Sinon.js
•
Message Queueing in Node.js with AWS SQS
•
Using Global Variables in Node.js
•
How to use module.exports in Node.js
Improve your dev
skills!
Get tutorials, guides, and dev jobs in your
inbox.
Enter your email
Sign Up
No spam ever. Unsubscribe at any time. Read our
Privacy Policy.
Scott Robinson
Author
© 2013-2022 Stack Abuse. All rights reserved.
Disclosure
Privacy
Terms
Download