zoukankan      html  css  js  c++  java
  • Delphi UTF编码/解码 UTF8Encode、UTF8Decode、URLEncode、URLDecode

    一、URL简介
        URL是网页的地址,比如 http://www.cnblogs.com。Web 浏览器通过 URL 从 web 服务器请求页面。
        由于URL字符串常常会包含非ASCII字符,URL在传输过程中,往往出现错误。因此,可以将非字符串字符,让一些特殊ASCII字符组合,代替非ASCII字符。这就是编码转换,当字符串传输后,可以返回原RUL字符串(解码)。
        URL只能使用 ASCII 字符集来通过因特网进行发送。URL编码,就是会将RUL字符转换为可通过因特网传输的格式。
        URL编码使用“%”其后跟随两位的十六进制数来替换非 ASCII 字符。比如“®”用“%A9”代替。
        URL不能包含空格。URL编码通常使用“+”来替换空格。

    二、RUL编码与解码
        1、uses HttpApp;   //引用单元

        2、编码,先UTF8编码,然后再URL编码,不然和标准的url_encode()编码结果不一致,查询结果自然不是预期的
        S2 := HttpEncode(UTF8Encode(S1));

        3、解码,先URL解码,然后再UTF8解码,否则结果是乱码。
        S1 := UTF8Decode(HttpDecode(S2));

     以上是内置函数调用

     

    三、URLEncode、URLDecode 

    //URLEncode
    function URLDecode(const S: string): string;
    var
      Idx: Integer;   // loops thru chars in string
      Hex: string;    // string of hex characters
      Code: Integer;  // hex character code (-1 on error)
    begin
      // Intialise result and string index
      Result := '';
      Idx := 1;
      // Loop thru string decoding each character
      while Idx <= Length(S) do
      begin
        case S[Idx] of
          '%':
          begin
            // % should be followed by two hex digits - exception otherwise
            if Idx <= Length(S) - 2 then
            begin
              // there are sufficient digits - try to decode hex digits
              Hex := S[Idx+1] + S[Idx+2];
              Code := SysUtils.StrToIntDef('$' + Hex, -1);
              Inc(Idx, 2);
            end
            else
              // insufficient digits - error
              Code := -1;
            // check for error and raise exception if found
            if Code = -1 then
              raise SysUtils.EConvertError.Create(
                'Invalid hex digit in URL'
              );
            // decoded OK - add character to result
            Result := Result + Chr(Code);
          end;
          '+':
            // + is decoded as a space
            Result := Result + ' '
          else
            // All other characters pass thru unchanged
            Result := Result + S[Idx];
        end;
        Inc(Idx);
      end;
    end;
    
    //URLDecode 
    function URLEncode(const S: string; const InQueryString: Boolean): string;
    var
      Idx: Integer; // loops thru characters in string
    begin
      Result := '';
      for Idx := 1 to Length(S) do
      begin
        case S[Idx] of
          'A'..'Z', 'a'..'z', '0'..'9', '-', '_', '.':
            Result := Result + S[Idx];
          ' ':
            if InQueryString then
              Result := Result + '+'
            else
              Result := Result + '%20';
          else
            Result := Result + '%' + SysUtils.IntToHex(Ord(S[Idx]), 2);
        end;
      end;
    end;
    

      

     

  • 相关阅读:
    JavaScript Patterns 5.7 Object Constants
    JavaScript Patterns 5.6 Static Members
    JavaScript Patterns 5.5 Sandbox Pattern
    JavaScript Patterns 5.4 Module Pattern
    JavaScript Patterns 5.3 Private Properties and Methods
    JavaScript Patterns 5.2 Declaring Dependencies
    JavaScript Patterns 5.1 Namespace Pattern
    JavaScript Patterns 4.10 Curry
    【Android】如何快速构建Android Demo
    【Android】如何实现ButterKnife
  • 原文地址:https://www.cnblogs.com/guorongtao/p/11975639.html
Copyright © 2011-2022 走看看