Base64 It's the most common one on the network for transmission 8Bit One of the encoding methods of bytecode ,Base64 It's based on 64 A method of representing binary data with two printable characters .
For the front end , All the places where you can put the path , Can be used base64 Instead .
node In buffer Objects can be accessed through toString(‘base64’) take buffer Object output as base64. Let's briefly describe how Chinese characters are converted into Chinese characters base64 The realization principle of the system .
For example, let's explore men " Ha " adopt base64 The conversion result of is "5ZOI" How is it achieved .
First, we use nodejs in Buffer Object from method , Turn a man into a man buffer object . Can get e5 93
88 Three 16 Binary byte . You can see that the letter is written by 3 It is composed of two bytes .
const buf = Buffer.from(" Ha "); console.log(buf); // // e5 93 88
Each byte is then converted to binary , 0x representative 16 Base system , We need to be here e5 93 88 Put them in front of each other 0x, toString Method can pass in the character base to be converted , It's written here 2.
// Convert each byte to binary (0xe5).toString(2); // 11100101 (0x93).toString(2); // 10010011 (
0x88).toString(2); // 10001000
And then there are three 2 Data are connected together , obtain 111001011001001110001000.
And then 111001011001001110001000 Cut in groups of six , It can be cut into 4 part .
111001 011001 001110 001000
Then use it in front of each group 0 Complement , manage to put together 8 position .
00111001 00011001 00001110 00001000
The four groups of data are converted into 10 Binary data . We can get it here 4 A number , Right? " Ha " This word translates into base64 It's got after 4 The two characters are a little closer .
parseInt('00111001', 2); // Binary to binary 10 Base system 57 parseInt('00011001', 2); // Binary to binary 10 Base system
25 parseInt('00001110', 2); // Binary to binary 10 Base system 14 parseInt('00001000', 2); //
Binary to binary 10 Base system 8
base64 The coding data is basically from the A-Z add a-z Plus 0123456789+/ From . We stitched this string together .
// base64 code let str = 'ABCDEFGHIGKLMNOPQRSTUVWSYZ'; str += str.toLowerCase();
// One stitching of lowercase characters str += '0123456789+/'; // Digital splicing once
We know that , String can be similar to an array by subscript to get the character at the specified position , Here, through the top 10 After conversion, the corresponding characters are obtained ,57 What we get is 5,25 What we get is Z,14 What we get is O,8 What we get is I.
Put these four characters together 5ZOI. It's the one after the conversion base64.
const base64 = str + str + str + str; // 5ZOI
<>base64 Translation steps
1, Passing string through Buffer.from obtain buffer, And will buffer Each of the 16 Of buffer Byte to 2 Base system
2, Put all the binaries together ,6 Divide them into groups , Fill in the front 00, manage to put together 8 Bit binary
3, Convert the new binary to 10 Base system
4, stay base64 Through the 10 Extraction of decimal subscript , And link