computer basic knowledge, The principle of base64

Computer basics, we are focusing on principles, why is this so?

What is base64

Base64 is one of the encoding methods for transmitting data in the network, especially for processing some non-printable characters into printable characters for convenient transmission.

How to encode base64

First make a simple base64 encoding here click, base64
We have base64 encoded the string hello

Original string: hello
After base64: aGVsbG8=
How do we learn step by step?

We must first understand the ASCII table, and check the ASCII value of each character, you can see ASCII table detailed explanation

first step
After review, we get the following:

Binary0110 10000110 01010110 11000110 11000110 1111

In the table above
The first line is the characters h e l l o
The second line is the decimal number of each character in the ASCII table
The third line is the binary number for each character in the ASCII table

Step 2

The binary string we get for the string is (the binary of each character is concatenated in turn):

0110 1000    0110 0101    0110 1100    0110 1100    0110 1111

third step

Group the 6-digit groups from left to right above

If there are less than 6 digits at the end, they are automatically filled with 0 to make up 6 digits to get the following:

011010    000110    010101    101100    011011    000110    1111 00

There are 4 digits left at the end of the string.

the fourth step

Add 00 before each 6-bit group above, and get the following:

00011010    00000110    00010101    00101100    00011011    00000110    00111100

the fifth step

Get its decimal representation for each 8 bits above:

26    6    21    44    27    6    60

Step Six

Look up the base64 character table:

IndexCorresponding CharacterIndexCorresponding CharacterIndexCorresponding CharacterIndexCorresponding CharacterIndexCorresponding Character

For the fifth step to get the string, check the base character table:

a G V s b G 8

Step Seven

Since the complement is used, we need to use = instead to indicate that we have used complement
= = 00, then == 0000
We made up the third step 00
So we get the last base64 string

Summarize base64 features

  1. As can be seen from the base64 character table, base64 can only include 64 printable characters such as 0-9a-zA-Z + /
  2. The number of bits of the original string is 8 * n (assuming n characters). After grouping by 6 bits, there will be less than 6 bits left.
  3. Due to (8 * n)% 6, the modulo operation may result in a value of 0 2 4 corresponding to the number of bits that need to be inadequate is 0 4 2 to make up 6
  4. A = corresponds to 00, so the end of the string after base64 may appear = or ==

base64 decoding

Base64 encoding is a reversible process, so decoding can be performed in the reverse order of encoding

Persist in original technology sharing, your support will encourage me to continue to create!