Base64 encoding and decoding for node. js learning, and node. jsbase64 Encoding

Source: Internet
Author: User
Tags string back

Base64 encoding and decoding for node. js learning, and node. jsbase64 Encoding

I. Base64 encoding Origin

Why is Base64 encoded? Because some network transmission channels do not support all bytes, for example, traditional mail only supports transmission of visible characters, control characters like ASCII codes cannot be transmitted through mail. In this way, the usage is greatly limited. Each byte of the binary stream cannot be all visible characters, so it cannot be transmitted. The best way is to make an extension scheme to support the transfer of binary files without changing the traditional protocol. The problem is solved by indicating that non-printable characters can also be used. Base64 encoding came into being. Base64 is a representation of binary data based on 64 printable characters.

Ii. Base64 encoding principles

Let's take A look at the Base64 index table, with 64 printable characters: "a-Z, A-z, 0-9, +, And. The value indicates the character index, which is specified by the standard Base64 protocol and cannot be changed. 64 characters can be expressed in 6 bits. A single byte has 8 bits, and the remaining two bits are wasted, so we have to sacrifice some space. Here we need to understand that a Base64 character is 8 bits, but the valid part is only 6 bits on the right, and the two on the left are always 0.

How can we use six valid bits to represent eight bits of traditional characters? The minimum public multiples of 8 and 6 are 24. That is to say, three traditional bytes can be represented by four Base64 characters to ensure that the valid digits are the same, in this way, the number of bytes is 1/3 more to make up that Base64 has only 6 Valid bits. You can also say that two Base64 characters can also represent a traditional character, but using the minimum public multiple solution is actually the least waste. It is easier to understand with the following figure. Man is a three-character string with a total of 24 valid bits. Therefore, 4 Base64 characters must be used to collect 24 valid bits. The red box indicates the corresponding Base64, and the six valid bits are converted to the corresponding index value and then corresponding to the Base64 signature table. The Base64 character corresponding to "Man" is "TWFU ". Speaking of this, there is a principle that you don't know if you find it. The minimum unit to convert to Base64 is three bytes. For a string, each conversion is three bytes and three bytes, it corresponds to four bytes of Base64. This is actually the case.

But what should you do if you find that the conversion is not three bytes at the end? The desire is finally realized. We can use two Base64 to represent A character or three Base64 to represent two characters. For example, A corresponds to only two binary bits of the second Base64, just fill in the four following zeros. Therefore, the Base64 character corresponding to A is QQ. As mentioned above, the principle is that the minimum unit of Base64 characters is a group of four characters. Then, there are only two characters, followed by two "=. In fact, you don't need to use "=" or delay decoding. The reason for using "=" may be that the Base64 string after multi-segment encoding will not cause confusion. It can be seen that only one or two "=" may appear at the end of the Base64 string, and "=" may not appear in the middle. The encoding process for the Chinese character "BC" is the same.

Iii. node. js common string encoding and decoding:

var b = new Buffer('JavaScript');var s = b.toString('base64');// SmF2YVNjcmlwdA==var b = new Buffer('SmF2YVNjcmlwdA==', 'base64')var s = b.toString();// JavaScript

Decodes and converts the code to hex.

var b = new Buffer('SmF2YVNjcmlwdA==', 'base64')var s = b.toString('hex');// 4a617661536372697074var b = new Buffer('4a617661536372697074', 'hex')var s = b.toString('utf8');// JavaScript

Iv. node. js encoding and decoding

var fs = require('fs');// function to encode file data to base64 encoded stringfunction base64_encode(file) {  // read binary data  var bitmap = fs.readFileSync(file);  // convert binary data to base64 encoded string  return new Buffer(bitmap).toString('base64');}// function to create file from base64 encoded stringfunction base64_decode(base64str, file) {  // create buffer object from base64 encoded string, it is important to tell the constructor that the string is base64 encoded  var bitmap = 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 stringvar base64str = base64_encode('kitten.jpg');console.log(base64str);// convert base64 string back to image base64_decode(base64str, 'copy.jpg');

Summary

The above is all about base64 encoding and decoding of node. js. I hope the content in this article will help you in your study or work. If you have any questions, please leave a message.

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.