In an interface docking, a base64 custom encoding table is used for encoding and decoding. I searched on the Internet and found that there are many and thorough explanations of the principles. The encoding examples are provided but there is no decoding. The following is my own implementation. An example of base64 custom dictionary decoding, which is relatively rough. After testing the assembly, there should be no problem. If you need this piece, you can take a look. First, take the principle from other people’s blogs
Base64 encoding, which is often used in our program development The encoding method used. It is a representation method based on using 64 printable characters to represent binary data. It is usually used as a encoding method for storing and transmitting some binary data! It is also a common encoding method for binary data represented by printable characters in MIME (Multipurpose Internet Mail Extensions, mainly used as an email standard)! It actually just defines a method of transmitting content using printable characters, and does not create a new character set! Sometimes, after we learn the idea of conversion, we can actually construct some of our own interface definition coding methods based on our own actual needs. Okay, let’s take a look at its conversion ideas!
Base64 implementation conversion principle
It is a method of using 64 printable characters to represent all binary data. Since 2 to the 6th power is equal to 64, every 6 bits can be used as a unit, corresponding to a certain printable character. We know that three bytes have 24 bits, which can correspond to 4 Base64 units, that is, 3 bytes need to be represented by 4 Base64 printable characters. Printable characters in Base64 include letters A-Z, a-z, and numbers 0-9, so there are 62 characters in total. In addition, the two printable symbols are generally different in different systems. However, the other two characters of Base64 that we often refer to are: "+/". The corresponding table of these 64 characters is as follows.
Number 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 21 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 /
When converting, three bytes of data are put into a 24-bit buffer one after another, and the byte that comes first occupies the high bit. If the data is less than 3 bytes, the remaining bits in the buffer will be filled with 0s. Then, take out 6 bits at a time and select the characters in <br>ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
according to their values as the encoded output. Continue until all input data is converted.
If there are two input data left at the end, add 1 "=" after the encoding result; if there is one input data left at the end, add 2 "=" after the encoding result; if there is no data left, nothing. Do not add, so as to ensure the accuracy of data restoration.
The encoded data is slightly longer than the original data, 4/3 of the original. No matter what kind of characters, all characters will be encoded, so unlike Quoted-printable encoding, some printable characters are retained. Therefore, it is not as readable as Quoted-printable encoding!
Text | M | a | n | |||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ASCII encoding | 77 | 97 | 110 | |||||||||||||||||||||
Binary 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 |
M’s The Ascii code 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 look at the other situation where it's not exactly 3 bytes!
Text (1 Byte) | A | |||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Binary bits | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | | Binary digit (0 complement) | ||||||||||||||
1 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | Base64 encoding | |||||||||||||
Q | = | = | Text (2 Byte) | |||||||||||||||||||||
C | Binary bit | |||||||||||||||||||||||
1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | 1 | x | x | x | x | 0 | 0 | 1 | 0 | ||
0 | 0 | 1 | 1 | 0 | 0 | x | x | x | x | x | x | Base64 encoding | Q | k | M | = |