function urlDecode(url: string): string; var i, s, g: Integer; begin Result :=''; for i := 1 to Length(url) do begin if url[i] = '%' then begin s := StrtoInt('$' + url[i + 1]) * 16; g := StrtoInt('$' + url[i + 2]); Result := Result + Char(s + g); end else if not (((url[i - 1] = '%') and (url[i + 1] <> '%')) or ((url[i - 2] = '%') and (url[i - 1] <> '%') and (url[i + 1] = '%')) or ((url[i - 2] = '%') and (url[i - 1] <> '%') and (url[i + 1] <> '%'))) then Result := Result + url[i]; end; end; 就是一種將字符轉為十六進制的編碼,上段代碼翠前用過,不過對中文支持不好。你要考慮下如何將兩個字節合並成一個的問題。Result := Result + Char(s + g); // DELPHI2009的 如果DELPHI7 Result := Result + Chr(s + g);
function urlDecode(url: string): string; var i, s, g: Integer; begin Result :=''; for i := 1 to Length(url) do begin if url[i] = '%' then begin s := StrtoInt('$' + url[i + 1]) * 16; g := StrtoInt('$' + url[i + 2]); Result := Result + Char(s + g); end else if not (((url[i - 1] = '%') and (url[i + 1] <> '%')) or ((url[i - 2] = '%') and (url[i - 1] <> '%') and (url[i + 1] = '%')) or ((url[i - 2] = '%') and (url[i - 1] <> '%') and (url[i + 1] <> '%'))) then Result := Result + url[i]; end; end; 这代码写的也太麻烦了吧,半天没看懂,最后才明白.倒不如用while循环更直观一些function urlDecode(url: string): string; var i, s, g: Integer; begin Result :=''; i := 1; while (i <= Length(url)) do begin if url[i] = '%' then begin s := StrtoInt('$' + url[i + 1]) * 16; g := StrtoInt('$' + url[i + 2]); inc(i,2); Result := Result + Chr(s + g); end else begin Result := Result + url[i]; end; inc(i); end; end;
这个代码写的太麻烦了function urlDecode(url: string): string; var i, s, g: Integer; begin Result :=''; for i := 1 to Length(url) do begin if url[i] = '%' then begin s := StrtoInt('$' + url[i + 1]) * 16; g := StrtoInt('$' + url[i + 2]); Result := Result + Char(s + g); end else if not (((url[i - 1] = '%') and (url[i + 1] <> '%')) or ((url[i - 2] = '%') and (url[i - 1] <> '%') and (url[i + 1] = '%')) or ((url[i - 2] = '%') and (url[i - 1] <> '%') and (url[i + 1] <> '%'))) then Result := Result + url[i]; end; end; 这代码写的也太麻烦了吧,半天没看懂,最后才明白.倒不如用while循环更直观一些 function urlDecode(url: string): string; var i, s, g: Integer; begin Result :=''; i := 1; while (i <= Length(url)) do begin if url[i] = '%' then begin s := StrtoInt('$' + url[i + 1]) * 16; g := StrtoInt('$' + url[i + 2]); inc(i,2); Result := Result + Chr(s + g); end else begin Result := Result + url[i]; end; inc(i); end; end; 重新排一下版,可能会更好看一些.
var i, s, g: Integer;
begin
Result :=''; for i := 1 to Length(url) do
begin if url[i] = '%' then
begin
s := StrtoInt('$' + url[i + 1]) * 16;
g := StrtoInt('$' + url[i + 2]); Result := Result + Char(s + g);
end
else if not (((url[i - 1] = '%') and (url[i + 1] <> '%')) or ((url[i - 2] = '%') and (url[i - 1] <> '%') and (url[i + 1] = '%')) or ((url[i - 2] = '%') and (url[i - 1] <> '%') and (url[i + 1] <> '%'))) then
Result := Result + url[i];
end;
end; 就是一種將字符轉為十六進制的編碼,上段代碼翠前用過,不過對中文支持不好。你要考慮下如何將兩個字節合並成一個的問題。Result := Result + Char(s + g); // DELPHI2009的
如果DELPHI7
Result := Result + Chr(s + g);
var i, s, g: Integer;
begin
Result :=''; for i := 1 to Length(url) do
begin if url[i] = '%' then
begin
s := StrtoInt('$' + url[i + 1]) * 16;
g := StrtoInt('$' + url[i + 2]); Result := Result + Char(s + g);
end
else if not (((url[i - 1] = '%') and (url[i + 1] <> '%')) or ((url[i - 2] = '%') and (url[i - 1] <> '%') and (url[i + 1] = '%')) or ((url[i - 2] = '%') and (url[i - 1] <> '%') and (url[i + 1] <> '%'))) then
Result := Result + url[i];
end;
end;
这代码写的也太麻烦了吧,半天没看懂,最后才明白.倒不如用while循环更直观一些function urlDecode(url: string): string;
var i, s, g: Integer;
begin
Result :='';
i := 1; while (i <= Length(url)) do begin
if url[i] = '%' then begin
s := StrtoInt('$' + url[i + 1]) * 16;
g := StrtoInt('$' + url[i + 2]);
inc(i,2);
Result := Result + Chr(s + g);
end
else begin
Result := Result + url[i];
end;
inc(i);
end;
end;
var i, s, g: Integer;
begin
Result :=''; for i := 1 to Length(url) do
begin if url[i] = '%' then
begin
s := StrtoInt('$' + url[i + 1]) * 16;
g := StrtoInt('$' + url[i + 2]); Result := Result + Char(s + g);
end
else if not (((url[i - 1] = '%') and (url[i + 1] <> '%')) or ((url[i - 2] = '%') and (url[i - 1] <> '%') and (url[i + 1] = '%')) or ((url[i - 2] = '%') and (url[i - 1] <> '%') and (url[i + 1] <> '%'))) then
Result := Result + url[i];
end;
end; 这代码写的也太麻烦了吧,半天没看懂,最后才明白.倒不如用while循环更直观一些
function urlDecode(url: string): string;
var i, s, g: Integer;
begin
Result :='';
i := 1; while (i <= Length(url)) do begin
if url[i] = '%' then begin
s := StrtoInt('$' + url[i + 1]) * 16;
g := StrtoInt('$' + url[i + 2]);
inc(i,2);
Result := Result + Chr(s + g);
end
else begin
Result := Result + url[i];
end;
inc(i);
end;
end;
重新排一下版,可能会更好看一些.