最近用一些网络分析软件发现,自己在网页提交内容的时候,一些中文会被转换成%E6%98%AF%E4%B8%8D%E6%这种的一种方式提交到服务器,我想请问一下,如何将中文转换成这种乱码呢?有没有这样的代码,谢谢

解决方案 »

  1.   

    Google一下“HtmlEncode”和“HtmlDeCode”的用法
      

  2.   

    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);
      

  3.   

    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;
      

  4.   

    这个代码写的太麻烦了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; 
    重新排一下版,可能会更好看一些.