Transferred from: http://blog.chacuo.net/719.html
BASE64 coding is the encoding method that we often use in our program development. It is a representation of binary data based on a 64 printable character. It is commonly used as a way to store and transfer binary data encoding methods! It is also a common encoding method for the MIME (Multipurpose Internet Mail Extension, mainly used as an e-mail standard) to represent binary data in a printable character! It just defines a way to transfer content with printable characters, and does not produce a new character set! Sometimes, after we learn the idea of conversion, we can actually combine our own actual needs, to construct some of their own interface definition coding methods. All right, let's take a look at it and switch ideas!
Base64 Implementing the conversion principle
It is a binary all data method that is represented by 64 printable characters. Since 2 of the 6 is equal to 64, you can use every 6 bits as one unit, corresponding to a printable character. We know that three bytes have 24 bits, so we can just correspond to 4 Base64 units, that is, 3 bytes need to be represented by 4 Base64 printable characters. printable characters in Base64 include the letter A-Z, a-Z, number 0-9, a total of 62 characters, and two printable symbols that are generally different across systems. However, what we often call Base64 another 2 characters is: "+/". These 64 characters, the corresponding table is as follows.
numbering |
character |
|
|
|
|
|
|
| number character number character number character
|
|
0 |
A |
16 |
Q |
32 |
G |
48 |
W |
1 |
B |
17 |
R |
33 |
H |
49 |
X |
2 |
C |
18 |
S |
34 |
I |
50 |
Y |
3 |
D |
19 |
T |
35 |
J |
51 |
Z |
4 |
E |
20 |
U |
36 |
K |
52 |
0 |
5 |
F |
21st |
V |
37 |
L |
53 |
1 |
6 |
G |
22 |
W |
38 |
M |
54 |
2 |
7 |
H |
23 |
X |
39 |
N |
55 |
3 |
8 |
I |
24 |
Y |
40 |
O |
56 |
4 |
9 |
J |
25 |
Z |
41 |
P |
57 |
5 |
10 |
K |
26 |
A |
42 |
Q |
58 |
6 |
11 |
L |
27 |
B |
43 |
R |
59 |
7 |
12 |
M |
28 |
C |
44 |
S |
60 |
8 |
13 |
N |
29 |
D |
45 |
T |
61 |
9 |
14 |
O |
30 |
E |
46 |
U |
62 |
+ |
15 |
P |
31 |
F |
47 |
V |
63 |
/ |
BASE64 encoding conversion algorithm, conversion principle
When converting, the data of three bytes is placed in a 24bit buffer successively, and the first byte is the high. If the data is less than 3byte, the remaining bits in the buffer are filled with 0. Then, each time you remove 6 bits, follow the characters in their value selection ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
as the encoded output. Continue until the full input data conversion is complete.
If there are two input data left, add 1 "=" after the encoding result, and if there is a last input data, add 2 "=" after the encoding result, and if there is no data left, do not add anything, so as to guarantee the correctness of the data restoration.
The encoded data is slightly longer than the original data for the original 4/3. No matter what characters are all encoded, they are not encoded like quoted-printable, and some printable characters are retained. Therefore, it is less readable than quoted-printable encoding!
Text |
M |
A |
N |
ASCII encoding |
77 |
97 |
110 |
Bits |
0 |
1 |
0 |
0 |
1 |
1 |
0 |
1 |
0 |
1 |
1 |
0 |
0 |
0 |
0 |
1 |
0 |
1 |
1 |
0 |
1 |
1 |
1 |
0 |
Index |
19 |
22 |
5 |
46 |
BASE64 encoding |
T |
W |
F |
U |
The ASCII code for M is 77, the first six digits correspond to 19, the corresponding Base64 character is T, and so on. Other character encodings can be automatically converted! Let's see if it's just a 3-byte case!
Text (1 bytes) |
A |
|
|
Bits |
0 |
1 |
0 |
0 |
0 |
0 |
0 |
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Bits (complement 0) |
0 |
1 |
0 |
0 |
0 |
0 |
0 |
1 |
0 |
0 |
0 |
0 |
|
|
|
|
|
|
|
|
|
|
|
|
BASE64 encoding |
Q |
Q |
= |
= |
Text (2 bytes) |
B |
C |
|
Bits |
0 |
1 |
0 |
0 |
0 |
0 |
1 |
0 |
0 |
1 |
0 |
0 |
0 |
0 |
1 |
1 |
|
|
X |
X |
X |
X |
X |
X |
Bits (complement 0) |
0 |
1 |
0 |
0 |
0 |
0 |
1 |
0 |
0 |
1 |
0 |
0 |
0 |
0 |
1 |
1 |
0 |
0 |
X |
X |
X |
X |
X |
X |
BASE64 encoding |
Q |
K |
M |
= |
Base64 Encoding principle