Base64 encoding and decoding of Node.js learning _node.js

Source: Internet
Author: User
Tags base64 control characters printable characters string back

I. ORIGIN of BASE64 coding

Why would there be Base64 code? Because some network transport channels do not support all bytes, for example, traditional messages only support the transfer of visible characters, such as ASCII code control characters can not be transmitted by mail. This use is subject to a lot of restrictions, such as the image of the binary stream of each byte can not be all visible characters, so it can not be transmitted. The best way to do this is to do an extension to support the transfer of binary files without changing the traditional protocol. The problem is solved by the ability to print characters that can be printed in a printable character. Base64 coding came into being, Base64 is a representation based on 64 printable characters representing binary data.

Two. Base64 Coding principle

Look at the Base64 index table, the characters are selected "A-Z, A-Z, 0-9, +,/" 64 printable characters. The value represents the index of the character, which is specified in the standard BASE64 protocol and cannot be changed. 64 characters with 6 bit bit can be all said, one byte has 8 bit bit, the remaining two bit is wasted, so have to sacrifice part of the space. What needs to be understood here is that a Base64 character is 8 bit, but the active part is only 6 bits to the right, and two to the left is always 0.

So how do you use 6 effective bit to represent the 8 bit of the traditional character? The LCM of 8 and 6 is 24, that is to say, 3 traditional bytes can be represented by 4 Base64 characters, guaranteeing the same number of significant digits, thus making up 1/3 of the bytes to compensate for the deficiency of the BASE64 only 6 valid bit. You can also say that a traditional character can be represented by two Base64 characters, but the solution to the LCM is to minimize waste. It is easier to understand the combination of the graphs below. Man is three characters, a total of 24 valid bit, had to use 4 Base64 characters to gather 24 significant digits. The red box is the corresponding base64,6 a valid bit into the corresponding index value and then corresponding BASE64 character chart, find "man" corresponding to the Base64 character is "Twfu". There's a principle here. No, you found it. The smallest unit to convert to Base64 is three bytes, each time a string is a three byte three byte conversion, corresponding to the Base64 four bytes. Well, that's pretty much it.

But how about converting to the end when you find it's not three bytes? Wish finally realized, we can use two Base64 to represent a character or three Base64 to represent two characters, like the next figure of a corresponding to the second Base64 bits only two, the four after 0. So a corresponds to the Base64 character is QQ. Above already said, principle is the smallest unit of Base64 character is four characters a group, that this only two characters, after two "=". In fact, do not "=" also do not delay the decoding, the reason for using "=", may be taken into account after a number of paragraphs encoded BASE64 string spell will not cause confusion. This shows that the Base64 string may only last appear one or two "=", the middle is impossible to appear "=". The encoding process for the character "BC" is also the same in the figure below.

Three, Node.js ordinary string encoding decoding:

var B = new Buffer (' JavaScript ');
var s = b.tostring (' base64 ');
smf2yvnjcmlwda==


var b = new Buffer (' smf2yvnjcmlwda== ', ' base64 ')
var s = b.tostring ();
Javascript

Encode and decode and turn into hex

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


var b = new Buffer (' 4a617661536372697074 ', ' hex ')
var s = b.tostring (' UTF8 ');
Javascript

Four, node.js encoded decoding picture

var fs = require (' FS ');

function to encode file data to Base64 encoded string
function Base64_encode (file) {
  //Read binary data
  VA R bitmap = fs.readfilesync (file);
  Convert binary data to Base64 encoded string return
  new Buffer (bitmap). toString (' base64 ');

function to create file from Base64 encoded string
function Base64_decode (base64str, file) {
  //create buffer Object from Base64 encoded string, it are important to tell the constructor of this string is Base64 encoded
  var bitm AP = new Buffer (base64str, ' base64 ');
  Write buffer to file
  fs.writefilesync (file, bitmap);
  Console.log (' ******** File created from Base64 encoded string ******** ');

Convert image to Base64 encoded string
var base64str = base64_encode (' kitten.jpg ');
Console.log (BASE64STR);
Convert base64 string back to Image 
Base64_decode (base64str, ' copy.jpg ');

Summarize

Above is the node.js of the Base64 encoding and decoding of all the content, I hope the content of this article for everyone's study or work to bring certain help, if you have questions you can message exchange.

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.