谁能告诉我哪里可以找到URL编码/解码的算法

barcley 2000-07-20 05:12:00
...全文
161 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zwok 2000-07-21
  • 打赏
  • 举报
回复
CString Decode(const char *str)
{
CString strDecoded ;
char ch, ch1, ch2 ;

const int nStrLen = strlen(str) ;

// special processing or query strings....
for(int ndx = 0 ; ndx < nStrLen ; ndx ++)
{
switch(ch = str[ndx])
{
case '+':
strDecoded += ' ' ;
break;
case '%':
ch1 = str[++ndx] ;
ch2 = str[++ndx] ; // 转换16进制字符串值为十六进制数字
ch1 = ch1 >= 'A' ? ch1 - 'A' + 10 : ch1 - '0' ;
ch2 = ch2 >= 'A' ? ch2 - 'A' + 10 : ch2 - '0' ;
ch1 = ch1 * 16 + ch2 ;
strDecoded += ch1 ;
break;
default:
strDecoded += ch;
}
}
return strDecoded;
}
/******************************************************************************
CString Encode(const char *str)
{
CString strEncoded, strHEX ;
const int nStrLen = strlen(str) ;
volatile char ch ;


for(int ndx = 0 ; ndx < nStrLen ; ndx ++)
{
ch = str[ndx] ;
if(ch == '@' || ch == '.' || (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
strEncoded += ch ;
else
{
strHEX.Format("%%%X", ch) ;
strEncoded += strHEX ;
}
}
return strEncoded ;
}
JGTM2000 2000-07-20
  • 打赏
  • 举报
回复
这是Delphi中的一段代码,供你参考:

function HTTPDecode(const AStr: String): String;
var
Sp, Rp, Cp: PChar;
begin
SetLength(Result, Length(AStr));
Sp := PChar(AStr);
Rp := PChar(Result);
while Sp^ <> #0 do
begin
if not (Sp^ in ['+','%']) then
Rp^ := Sp^
else
if Sp^ = '+' then
Rp^ := ' '
else
begin
inc(Sp);
if Sp^ = '%' then
Rp^ := '%'
else
begin
Cp := Sp;
Inc(Sp);
Rp^ := Chr(StrToInt(Format('$%s%s',[Cp^, Sp^])));
end;
end;
Inc(Rp);
Inc(Sp);
end;
SetLength(Result, Rp - PChar(Result));
end;

function HTTPEncode(const AStr: String): String;
const
NoConversion = ['A'..'Z','a'..'z','*','@','.','_','-',
'0'..'9','$','!','''','(',')'];
var
Sp, Rp: PChar;
begin
SetLength(Result, Length(AStr) * 3);
Sp := PChar(AStr);
Rp := PChar(Result);
while Sp^ <> #0 do
begin
if Sp^ in NoConversion then
Rp^ := Sp^
else
if Sp^ = ' ' then
Rp^ := '+'
else
begin
FormatBuf(Rp^, 3, '%%%.2x', 6, [Ord(Sp^)]);
Inc(Rp,2);
end;
Inc(Rp);
Inc(Sp);
end;
SetLength(Result, Rp - PChar(Result));
end;

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧