base64 encoding

Code URL: http://www.cnblogs.com/hongru/archive/2012/01/14/2321397.html


1. Get the string (ASCII code)
2. Convert to decimal (corresponding to ASCII code table)
3 , Convert to binary 8bit byte (corresponding to decimal, binary code table)
4. Convert to binary 6bit byte (using shift)
5. Convert 6bit binary to decimal (Corresponding to the encoding table)
6. Convert the decimal number to BASE64 encoding (compare with the BASE64 encoding table).





The underlying implementation principle is

1. When converting binary 8bit to 6bit,

  1 is required, and the first original character is shifted to the right by two bits. The first target character
  2, the first original minus the remaining two bits and 0x03 (this is the hexadecimal representation, converted to binary is 00000011) perform & operation, and then shift right by 4 bits, in phase or Shift the second original character by four bits to the right to get the second target character
  3. At this time, the remaining four bits of the second original character and 0x0F (this is the representation of hexadecimal, and it is 00001111 when converted to binary) Perform the & operation, phase or right shift the third original character of two digits, and get the third target character
  4. At this time, the third original character has six remaining digits, and 0x3F (this is the hexadecimal representation, converted into Binary is 00111111) for & operation. To get the fourth target character


2, when the binary 8bit is converted to the binary 6bit, check whether the characters are a group of 4, and 24bit. If it is not enough, you need to add 0. Make up for 24 bits.



Here is my personal opinion:
In fact, all encodings are because each encoding manufacturer does not want to use the existing encoding rules on the market, and then formulates a set of specifications, just read the string and convert it to binary, and then re-standardize the string according to their encoding rules. Can. To put it bluntly, it is a variety of base conversions to convert.


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326886156&siteId=291194637