16,749
社区成员
发帖
与我相关
我的任务
分享
function Encode64(const test:string): string;
var
s, 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);
//将字符串的十六进制数以字符串的形式表示出来
case (length(s) mod 3) of
0: addnum := 0;
1:
begin
s := s + '00';
addnum := 2;
end;
2:
begin
s := s + '0';
addnum := 1;
end;
else
addnum := 0;
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;
function Encode64Fast(const Input : AnsiString): AnsiString;
var
pDest , pSour : PChar;
i , Count : integer;
w : DWORD;
B : Boolean;
begin
if Input='' then begin
Result := '';
exit;
end;
SetLength(Result , Length(Input)*3 DIV 2 + 100);//预设长度
pDest := Pointer(Result);
pSour := Pointer(Input);
Count := 0;
B := False;
for i:=1 to (Length(Input)+1)*2 DIV 3 do begin //计算部分
w := ((PByte(pSour)^ SHL 8) or (PByte(pSour+1)^));//不支持BSWAP,因此只能这样了
inc(pSour);
if not B then w := w SHR 4
else begin
w := w AND $0FFF;
inc(pSour);
end;
B := Not B;
pDest^ := BaseTable[(w SHR 6 ) + 1]; //代替 DIV 64
inc(pDest);
pDest^ := BaseTable[(w and 63) + 1]; //代替 MOD 64
inc(pDest);
inc(Count , 2);
end;
case (Length(Input)*2 mod 3) of
1: (pDest-1)^ := '=';
2: begin
pDest^ := '=';
inc(pDest);
pDest^ := '=';
inc(Count , 2);
end;
end;
Delete(Result , Count+1 , Length(Result));
end;