Base64 principle Analysis

Source: Internet
Author: User
Tags printable characters

I. BASE64 code ORIGIN

Why do you have Base64 code? Because some network transport channels do not support all bytes, such as traditional mail only supports the transmission of visible characters, such as ASCII code control characters can not be transmitted by mail. The use of this is greatly limited, and it is not possible to send all of the bytes of the binary stream as visible characters. The best way to do this is to do an extension to support the delivery of binary files without changing the traditional protocol. The non-printable characters can also be printed with printable characters, the problem is solved. Base64 coding comesinto being, Base64 is a representation method of binary data based on 64 printable characters .

Two. Index table for Base

Take a look at the index table of the Base64, with the characters selected as "A-Z, A-Z, 0-9, +,/" 64 printable characters. The numeric value represents the index of the character, which is specified by the standard BASE64 protocol and cannot be changed.

Three. Principle of Base64

Base64 's Code table is only 64 characters, if you want to express 64 characters, use 6 bytes can be fully represented (2 6 is 64).

Because the encoding of Base64 is only 6 bytes, while the normal characters are denoted by 8 bytes, 8 and 6 least common multiple are 24, so 4 Base64 characters can represent 3 standard ASCLL characters;

If the string is converted to Base64 code, the corresponding string will be converted to the ASCLL Code table corresponding to the number, and then convert the number to 2, such as a ASCLL code flavor 97, 97 of the binary is: 01100001, 8 binary extracted into 6, The remaining 2 binary and the back of the binary continue splicing, and finally the 6 binary code converted to BASE64 for the encoding, the following is the specific parsing process case:

The process of converting ABC three characters to Base64

string      a       b        cASCII            98       8bit bytes    01100001 01100010 01100011 6bit bytes    011000   010110   001001   100011 decimal        -       a        9         * corresponding Code    Y        W        J        J

The process of converting man three characters to Base64

  string m a nascii  109  97   8bit byte  01101101  01100001  01101110  6bit bytes  011011  010110  000101  101110   decimal  27  22  5  46   corresponding code b W F U  

Now there is a small problem, when the conversion to the last, the last character of less than 3 characters to do, if less than three characters, we directly at the end of the add = number can be referred to the following two string conversion case:

The data URI currently supports many types:


Currently, the data URI scheme supports the following types: data:, Text data data:text/plain, text data data:text/html,html code DATA:TEXT/HTML;BASE64, Base64 encoded HTML code DATA:TEXT/CSS,CSS code data:text/css;base64,base64 encoded CSS code data:text/javascript,javascript code data: TEXT/JAVASCRIPT;BASE64,BASE64 encoded JavaScript code DATA:IMAGE/GIF;BASE64,BASE64 encoded GIF image data data:image/png;base64, Base64 encoded PNG image data DATA:IMAGE/JPEG;BASE64,BASE64 encoded JPEG image data DATA:IMAGE/X-ICON;BASE64,BASE64 encoded icon image data

Base64 simply, it translates some 8-bit data into standard ASCII characters, and currently, IE8, Firfox, Chrome, and opera browsers support this small file embedding.

Four references:

Coding principle of Base64: http://www.cnblogs.com/hongru/archive/2012/01/14/2321397.html

Base64 byte code principle: Http://www.cnblogs.com/chengxiaohui/articles/3951129.html

Base64 principle Analysis

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.