Base64 encoding based on decimal implementation

zhaozj2021-02-16  95

I. The principle of Base64 encoding is very simple. The method is that the method is to take the input data flow each time 6 bit (1 binary per bit represents 1 binary), less than 6bit, this, every 3 8th bytes The encoded is 4 6-bit bytes (3 × 8 → 4 × 6); "=" is filled with "=" dissatisfied. In fact, these four six bytes remain 8, but the high two digits are set to 0. When one byte is only 6-bit valid, its value space is 0 to 2, 63, that is to say that each encoded space for each encoded of the converted Base64 is (0 ~ 63) . In fact, the ASCII code between 0 to 63 has many invisible characters, so I should be a mapping, the mapping table (code table) is

This allows 3 8-bit bytes to convert to 4 visible characters. That is to say, the conversion string is more than 1/3 of the original, and the expansion ratio is 3: 4. For example: 1. When the string character is 3 times; such as a string "ABC", its hexadecimal in the computer memory is $ 41, $ 42, $ 43, decimal as "65" "66 "67"; binary is expressed as 01000001 01000010 01000011 to take this three binary numbers to 6bit, 010000/01 0100/0010 01000011 Convert to: 010000 010100 001001 000011 Transfer these four binary numbers into a 16 system The number is: $ 10, $ 14, $ 9, $ 3, and the decimal number is 16, 20, 9, 3. In the above code table, find the corresponding characters respectively Q, U, J, D. It is also to say that the string "ABC" is "qujD" after the base64 encoding. This is the simplest case, that is, the ASCII code character number is just 3. The rest of the remainder is 2 and 1. 2, when the remainder is 2, such as a string "CE", its hexadecimal in memory is $ 63, $ 65; decimal representation represents 99,101; binary is expressed as 01100011 011000/11 0110/0101 At this time, the third character is less than 6 bits, and the back is completed, that is, 0101 becomes 010100. The conversion result is 011000 110110 010100 These three binary numbers converted to a hexadecimal number of $ 18, $ 36, $ 14; decimal digits are 24, 54, 20. The control code table derived "Y2U". The number of characters after encoding is less than 4 bits, with "=" fill, and finally encoded "Y2U =". 3, when the remainder is 1, such as the string "{", its hexadecimal in memory is $ 7B, decimal 123, binary position is 01111011 followed 6BIT 01110/11 to supplement 011110 / 110000 conversion results are 011110 and 110000 These two binary numbers converted to a hexadecimal number of $ 1e, $ 30, and a decimal number of 30, 48. The results of the control code are "EW", which make up "=", and finally the encoding is "EW = =". The decoding is also very simple, is the inverse process of the encoding, that is, the number of binary numbers of 6bit, then recombinantly, and seizes the original code.

Second, the Base64 encoding implementation method Base64 encoded has a lot of implementation, but most of the shift, and or operation of the binary number is inconvenient. To this end, I have been exploited and came up with a decimal coding method based on decimal. According to the above principles, each 3 bytes will be converted to 4 bytes, other, each semi-byte (12bit) is just converted to 2 encoding characters (2 × 6bit). Use 0 ~ f to represent 0 ~ f at the lower half of the hexadecimal. To this end, the three characters after the string under hexadecimal indication is taken in turn, and the two characters after the encoding is easily obtained by tightening and dividing the operation. For example, string ABC, its hexadecimal represents $ 414243 in memory, with 3 digits, resulting in $ 414 and $ 243, converted into a decimal 1044 and 579. These two numbers were tied to 64, respectively, and was divided into operation 1044 Div 64 = 16 1044 MOD 64 = 20 579 DIV 64 = 9 579 MOD 64 = 3 results in 16, 20, 9, 3. The results of the codes in the control are also "quJD". In the advanced language, it is easy to express the hexadecimal of the string in string, and then convert to an integer by intercepting 3 characters in turn, finally performing the above calculation. To this end, I have written a set of Base64 encoding in the Delphi language implementation source code: // Defines the code table constbasetable: string = 'AbcdefghijklmnopqrStuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 /';

// Encoding program Function Strtobase64 (const test: string): String; Vars, S1: String; I, P, Len, N, Addnum: Integer; Begin Result: = '; s: =' '; for i: = 1 to Length (TEST) Do S: = S INTTOHEX (ORD (TEST [I]), 2); // The hexadecimal number of the string is represented in the form of a string (LENGTH (S) MOD 3) of 0: addnum: = 0; 1: Begin S: = S '00 '; addnum: = 2; End; 2: Begin S: = S ' 0 '; addnum: = 1; end; end; len: = Length (s) DIV 3; for i: = 1 to Len Do Begin S1: = MIDSTR (S, I * 3-2, 3); P: = STRTOINT ('$' S1); N: = P Div 64; Result: = Result BaseTable [N 1]; N: = P MOD 64; Result: = Result BaseTable [N 1]; END; if Addnum = 1 Then Result: = Result '=='; if Addnum = 2 Then Result [Length (Result)]: = '='; END;

转载请注明原文地址:https://www.9cbs.com/read-11561.html

New Post(0)