Base-64 encoded

Base64 is one of the most common network for the transmission of 8Bit encoding byte code, we can see RFC2045 ~ RFC2049, MIME above detailed specification.
Base64 requires the byte of each byte is converted to three 8Bit 6Bit of four (= 4 * 3 8 * 6 = 24), then add another two high 6Bit 0, 8Bit four bytes of the composition, i.e. that string theory converted to 1/3 than the original length.
So that it would not be too abstract? Not afraid, we look at an example:

Conversion ago aaaaaabb ccccdddd eeffffff  
After conversion 00aaa 00bbcccc 00ddddee 00ffffff

It should be clear, right? The above three bytes of the original text, the following four bytes are converted Base64 encoding, which are the first two 0.
After the conversion, we use a stopwatch to get the string (that is, the final Base64 encoding) we want, this table is taken from this :( RFC2045)
                            the Table 1: at The Base64 Alphabet
      Value Encoding Encoding Value Value Value Encoding Encoding
           0 A. 17 R & lt 34 is I 51 is Z
           . 1 B 18 is S 35 J 52 is 0
           2 C. 19 T 36 K 53 is. 1
           . 3 D 20 is the U-37 [L 54 is 2
           . 4 E 21 is V 38 is m 55. 3
           . 5 F. 22 is W is 39 n-56 is. 4
           . 6 G 23 is X 40 o 57 5
           . 7 H 24 the Y 41 is P 58. 6
           . 8 the I 25 the Z 42 is Q 59. 7
           . 9 J 26 is A 43 is R & lt 60. 8
          10 K 27 B 44 is S 61 is. 9
          . 11 L 28 C 45 T 62 is +
          12 is M 29 D 46 is U 63 is /
          13 is N E V 47 30
          14 W 48 O 31 is F (PAD) =
          15 G 49 P 32 X
          16 Q 50 Y 33 is H
let us look at a practical example, a deeper impression!

Conversion ago 10101101 10111010 01110110  
After conversion 00101011 00011011 00101001 00110110
Decimal 43 27 41 54
The code table corresponding to the value r b p 2

Therefore, the above Base64 24, encoding is rbp2
decoding Similarly, the bits on the re-connected rbq2 recombinant obtained three 8-bit values, obtained the original code.
(Decoding coded just the reverse process, and I do not say the other related MIME RFC still has a lot of, if you need to find your own details.)
By programming closer to the thinking, the encoding process is like this:
the first character is obtained by right-shifting a first target character position Base64 table 2, to take the corresponding character table based on this value, the target is the first character.
Then the first character left four plus four second character to the right, that is to get the second goal of characters.
Then the second character left two plus a third character, right 6, get a third goal character.
Finally, the right to take a third character that is six characters to get the fourth goal.
After each of the above steps, then the result of the AND bit operation is 0x3F, the character can be obtained after the encoding.
So easy! That's all !!!
But the wise etc ...... you may ask, should the original number of bytes is a multiple of three, ah, if this condition is not met, then how to do it?
Our solution is this: not enough to place the original byte 0 can be used to make up the whole, Base64 encoding = number instead of the conversion. This is the reason why some will be Base64 encoding one or both ends of the equal sign, but only a maximum of two equal sign. Because:
the remainder MOD 3 = original number of bytes
so the remainder is 0 in any case could only one of these three numbers. If the remainder is zero, then it indicates the number of bytes description is just a multiple of 3 (best case it). If it is 1, Base64 encoding to make a multiple of 4, we must make two equal sign; Similarly, if it is 2, is necessary to make an equal sign.
In talking about this, and everybody should all understand, right? If there is not clear, then go back and take a closer look, it is not difficult to understand.
Here I give a demo Base64 encoding / decoding procedures, hoping to be useful to you. I also hope to help you to improve it and use it to make more use, do not forget to let me know when ah! (I'm too busy)
PS Why take the name Base64:
        Base64 encoding in this way can make the text or picture can also be successfully transmitted over the Internet. After BASE64 encoded string contains only case letters, Arabic numerals, plus the backslash, substantially a total of 64 characters, do not contain other special characters, which thus named BASE64. Encoded string plus about 1/3 than the original string length.

Reproduced in: https: //www.cnblogs.com/henryhappier/archive/2010/09/02/1816253.html

Guess you like

Origin blog.csdn.net/weixin_33767813/article/details/93537625