Base64 encoding/decoding in node. js

Source: Internet
Author: User
Tags base64

"Base64 encoding/decoding in node. js"

Here's how you encode normal text to base64 in node. JS:

var=newBuffer(' JavaScript ');
var= b. ToString(' base64 ');
smf2yvnjcmlwda==

And here's how you decode Base64 encoded strings:

var=newBuffer(' smf2yvnjcmlwda== ',' base64 ')
var= b. ToString();
Javascript

If you is interested in the details of how the above examples worked, follow me.

The new Buffer() constructor requires a number, array or string as the first parameter, and an optional encoding type as the Seco nd parameter. The possible encoding types is ASCII, UTF8, UCS2, base64, binary, and hex; The default being UTF8.

By passing the second parameter, we tell JavaScript, the "the string you see was encoded in this particular format". Notice how we do that in the decoding example.

Once we have the encoded string and we call the toString() method on the string. If we don ' t pass the encoding type toString() to, JavaScript assumes we want to convert the object to UTF8 encoded string by de Fault. We can make it convert to other formats by passing the encoding type to toString() .

Reference: http://www.hacksparrow.com/base64-encoding-decoding-in-node-js.html

Base64 encoding/decoding in node. js

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.