Base64 encoded Delphi algorithm!

xiaoxiao2021-03-06  43

Base64 encoded Delphi algorithm! Learn the base64 encoding algorithm

procedure TForm1.Button1Click (Sender: TObject); var_1byte, _2byte: array of byte; strlen, i, j, jj: integer; str: string; constbasetable = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 /'; beginstr: = 'abcabcde'; // str: = 'abcabc'; strlen: = Length (STR); setlength (_1byte, strlen); if Strlen Mod 3> 0 THEN SETLENGTH (_2byte, (Strlen Div 3) * 4 Strlen MOD 3) 1) Else SetLength _2byte, (Strlen Div 3) * 4 (Strlen MOD 3) 0);

For i: = 0 to strlen-1 dobegin_1byte [i]: = ORD (STR [i 1]); end; j: = 0; jj: = 0; for i: = 0 TO (Strlen Div 3) -1 Dobeginj: = i * 3; _2byte [jj]: = _ 1byte [j] shr (2); // a-> a; inc (jj); _ 2byte [jj]: = byte (_1byte [j] shl (6) ) SHR (2) _1byte [J 1] shr (4); // a & b-> binc (jj); _ 2byte [jj]: = byte (_1byte [j 1] shl (4)) Shr (2) _1byte [J 2] shr (6); // B & C-> Cinc (jj); _ 2byte [jj]: = byte (_1byte [j 2) SHL (2)) Shr (2); Inc (jj) END; J: = (Strlen Div 3) * 3; Case (Strlen Mod 3) OF1: Begin_2byte [JJ]: = _ 1byte [J] SHR (2); // A-> A; Inc (JJ); _ 2byte [JJ]: = Byte (_1byte [J] SHL (6)) Shr (2); // A & B-> Bend; 2: Begin_2byte [JJ]: = _ 1byte [J] Shr (2); // A-> A; Inc (jj); _ 2byte [jj]: = byte (_1byte [j] shl (6)) Shr (2) _1byte [j 1] shr (4); // a & b-> binc (jj); _2byte [jj]: = byte (_1byte [j 1] SHL (4)) shr (2); // b2-> cend; end; // According to the code! Str: = '; for i: = 0 to high (_2byte) Dobeginstr: = STR COPY (BaseTable, _2byte [i] 1, 1); END; SHOWMESSAGE (STR);

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

New Post(0)