Home>Article> What is the principle of base64 encoding?

What is the principle of base64 encoding?

coldplay.xixi
coldplay.xixi Original
2020-07-01 15:00:01 3844browse

The base64 encoding principle is to disassemble an 8-bit subsection sequence into 6-bit fragments, and assign a character to each 6-bit fragment. These 64 characters are lowercase letters [a-z] and uppercase letters [A-Z] ], numbers [0-9], symbols [ ], [/].

What is the principle of base64 encoding?

The principle of base64 encoding is:

Base64 encoding disassembles an 8-bit subsection sequence into 6-bit fragments. And allocate a character to each 6-bit fragment. These 64 characters-lowercase letters a-z, uppercase letters A-Z, numbers 0-9, symbols " ", "/". See here to know what 64 means. Well, it represents 64 characters. In fact, there is a pad "=", which is actually 65 characters.

Base64 rules are as follows:

The first step is to treat every three bytes as a group, a total of 24 binary bits.

The second step is to divide these 24 binary bits into four groups, each group has 6 binary bits.

The third step is to add two 00s in front of each group to expand it into 32 binary digits, that is, four bytes. Therefore, the base64 encoded string is about 33% larger than the original one

Example

Give a specific example to demonstrate how the English word Man is converted into Base64 encoding.

What is the principle of base64 encoding?

In the first step, the ASCII values of "M", "a", and "n" are 77, 97, and 110 respectively, and the corresponding binary values are 01001101 , 01100001, 01101110, concatenate them into a 24-bit binary string 010011010110000101101110.

The second step is to divide this 24-bit binary string into 4 groups, each group has 6 binary digits: 010011, 010110, 000101, 101110.

The third step is to add two 00s in front of each group to expand it into 32 binary digits, that is, four bytes: 00010011, 00010110, 00000101, 00101110. Their decimal values are 19, 22, 5, and 46 respectively.

The fourth step, according to the figure below, get the Base64 encoding corresponding to each value, namely T, W, F, u.

Therefore, Man’s Base64 encoding is TWFu.

What is the principle of base64 encoding?

The above is the detailed content of What is the principle of base64 encoding?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn