node. js BASE64 encoding and decoding

Source: Internet
Author: User
Tags encode string

How do I encode a string in node. js? Is it as simple as using Base64_encode () in PHP?

There are many ways to encoding strings in node. js, rather than defining a variety of global functions as in JavaScript. Here's how to encode a normal string into Base64 format in node. JS:

var New Buffer (' JavaScript '); var s = b.tostring (' base64 '); // smf2yvnjcmlwda==

The following is the code for the decode Base64 string:

var New Buffer (' smf2yvnjcmlwda== ', ' base64 ')var s = b.tostring (); // JavaScript

If you want to know the implementation details of the above code, please look down.

The first parameter of the constructor new Buffer () can be either a number,array or a string. The second parameter is an optional parameter that represents the type of encode, which can be ASCII, Utf8, UCS2, Base64, Binary, or hex. The default value is Utf8.

The second argument tells the program which particular format the given string is encode in. Notice the arguments we passed in the example above decode.

We use the toString () method to convert the encode string to another format, which defaults to Utf8. Specify different parameters that can be converted to the format we want. For example, we can convert a string after Base64 into hex format:

var New Buffer (' smf2yvnjcmlwda== ', ' base64 ')var s = b.tostring (' hex '); // 4a617661536372697074

And then decode it into a string that human beings can read in the following way:

var New Buffer (' 4a617661536372697074 ', ' hex ')var s = b.tostring (' UTF8 '); // JavaScript

Once you have mastered the basic buffer and encode, we can encode the file into a Base64 string using node. js's file module.

varFS = require (' FS ');//function to encode file data to Base64 encoded stringfunctionBase64_encode (file) {//Read binary data    varBitmap =fs.readfilesync (file); //convert binary data to Base64 encoded string    return NewBuffer (bitmap). toString (' base64 ');}//function to create file from Base64 encoded stringfunctionBase64_decode (base64str, file) {//create buffer object from Base64 encoded string, it's important to tell the constructor and the string is base64 en Coded    varBitmap =NewBuffer (Base64str, ' base64 '); //write buffer to filefs.writefilesync (file, bitmap); Console.log (' ******** File created from Base64 encoded string ******** ');}//convert image to Base64 encoded stringvarBase64str = Base64_encode (' kitten.jpg ')); Console.log (BASE64STR);//Convert base64 string back to imageBase64_decode (base64str, ' copy.jpg ');

The Ps:utf8 is an ASCII superset. If you use only the characters on the standard English keyboard, you can use ASCII encoding, but if you are dealing with other "exotic" characters or symbols, such as, こんにちは,üdvözöljük, etc., use UTF.

node. js BASE64 encoding and decoding

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.