25, Base64

Source: Internet
Author: User

Base64 requires that every three bytes of 8Bit be converted to four 6Bit bytes (3*8 = 4*6 = 24), and 6Bit is then added two bits high 0, which makes up four 8Bit bytes, that is, the converted string will theoretically be 1/3 longer than the original one. Rules about this code: ①. 3 characters into 4 characters. ② every 76 characters multibyte a newline character. ③. The final terminator should also be dealt with. Example (1) conversion of the first 11111111, 11111111, 11111111 (binary) after conversion 00111111, 00111111, 00111111, 00111111 (binary) above three bytes is the original text, The following four bytes are the converted Base64 encoding, with the first two bits being 0.

Example (2) conversion before 10101101,10111010,01110110 conversion 00101011, 00011011, 00101001, 1.1011 million binary 43 27 41 54 The value in the corresponding Code table R B p 2 so the above 24-bit encoding, the encoded Base64 value for RBP2 decoding the same, the RBQ2 bits connection and re-recombination to get three 8-bit value, the original code. (decoding is just the inverse of the encoding, there are many RFCs about MIME, if you need more information, please find it yourself.) )

The first byte, which is processed according to the first byte of the source byte. Rule: The first byte of the source is shifted right by two bits, the low 2 bits are removed, and the high 2 bits are 0. Both: 00 + high 6 bits, two bytes, combined with the first byte of the source byte and the second byte. As a rule, the first byte high 6 bits is removed and then left four bits, the second byte moves right four bits namely: source first byte low 2 bit + source 2nd byte High 4 bits three bytes, according to the second byte of the source byte and the third byte Union processing, the rule second byte removes the high 4 bits and moves left two bits (high 6 bits), The third byte moves 6 bits to the right and removes the high 6 bits (2 bits lower), adds the fourth byte, the rule, the source third byte removes the high 2 bits.//With a more programming-like thinking, the encoding process is://The first character gets the Base64 table position of the first target character by moving the 2-bit right. Based on this value, the corresponding character on the table is the first//target character. The first character is then associated with the (&) operation with 0x03 (00000011) and the left 4 bits, then the second word aligns the 4 bit with the former or (|), that is, the second target character is obtained. Then the second character with 0x0f (00001111) and the (&) operation and left 2 bits, then the third word aligns shift 6 bit to the former phase or (|), to obtain the third target character. Finally, the third character (&) is performed with 0x3f (00111111) to obtain a fourth target character. After each of the above steps, and then the results with the 0x3F operation, you can get the encoded characters. But wait ... Smart you may ask, the original number of bytes should be a multiple of 3 ah, if the condition is not satisfied, then what to do? Our solution is this: the remaining bytes of the original text continue to go separately according to the coding rules (1 change 2,2 to 3; not enough digits with 0 completion), and then fill 4 bytes with the = number. This is why some Base64 encodings end with one or two equals signs, but the equals sign is only two. because: Remainder = original number of bytes MOD 3 so the remainder can only be one of the three 0,1,2 in any case. If the remainder is 0, it means that the original byte count is exactly a multiple of 3 (ideally). If it is 1, turn to 2 Base64 encoded characters, in order to let the BASE64 encoding is a multiple of 4, it is necessary to fill 2 equal signs, similarly, if 2, it is necessary to fill 1 equal signs. After the conversion, we use a code table to get the string we want (that is, the final Base64 encoding), this table is this:








25, Base64

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.