URLCODETOGB

来源:互联网 发布:"淘宝差评师曝光平台 编辑:程序博客网 时间:2024/05/18 07:12
function URLDecode(const S: string): string;
var
  Idx: Integer;
  Hex: string
  Code: Integer;
begin  
  Result := '';
  Idx := 1;
  while Idx <= Length(S) do
  begin
    case S[Idx] of
      '%':
      begin
        if Idx <= Length(S) - 2 then
        begin
          Hex := S[Idx+1] + S[Idx+2];
          Code := SysUtils.StrToIntDef('$' + Hex, -1);
          Inc(Idx, 2);
        end
        else
          Code := -1;
         if Code = -1 then
          raise SysUtils.EConvertError.Create(
            'Invalid hex digit in URL'
          );
        Result := Result + Chr(Code);
      end;
      '+':
        Result := Result + ' '
      else
        Result := Result + S[Idx];
    end;
    Inc(Idx);
  end;
end;
 
 
function URLEncode(const S: stringconst InQueryString: Boolean): string;
var
  Idx: Integer
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;
原创粉丝点击