现在编程由于要用到对html网页加密,于是查了n多关于escape()函数资料,但是没有一个能正确说出个方法,只说转换成16进制。但是escape的汉字是unicode编码的,所以硬性的把html的源码转换成16进制,经过unescape()后汉字是乱码。
delphi提供两个函数 httpencode()和httpdecode(),经过研究,它和escape()和unescape()差别很大。一个是字符集的问题,还有一个是httpencode()转换了许多escape()不转换的字符。比如:'/'等。
经过1天的尝试,由于屡试不成功,中间都想放弃了尝试了,但是最后终于找出了一个方法。下面贴出我的源代码,以供大家参考,在遇到这个问题时,就不用费尽精力查资料和尝试了:Function TYoungCHDisp.HtmlEncrypt(FilePath:String): Boolean;
var
I: integer;
Tmp1,Tmp2: TStringList;
S: String;
begin
Tmp1 := TStringList.Create;
Tmp2 := TStringList.Create;
If Not FileExists(FilePath) Then Exit;
Tmp1.LoadFromFile(FilePath);
S := '';
For I := 0 to Tmp1.Count - 1 do
begin
S := S + Encode(Tmp1.Strings[I]) + '%0D%0A';
end;
S := 'document.write(unescape("' + S + '"));';
Tmp2.Add('<script>');
Tmp2.Add('<!--');
Tmp2.Add(S);
Tmp2.Add('//-->');
Tmp2.Add('</script>');
Tmp2.SaveToFile(FilePath);
Tmp1.Free;
Tmp2.Free;
end;Function TYoungCHDisp.Encode(S: String): String;
const
NoConversion = ['A'..'Z','a'..'z','0'..'9','/'];
var
T: WideString;
I: Integer;
H,L: Byte;
begin
Result := '';
T := WideString(S);
For I := 1 to Length(T) Do
begin
if (Char(T[i]) in NoConversion) and (Ord(T[I]) < 127) Then
begin
Result := Result + T[I];
Continue;
end;
H := Hi(Ord(T[I]));
L := Lo(Ord(T[I]));
If Ord(T[I]) > 127 Then
Result := Result + '%u' + (IntToHex(H, 2) + IntToHex(L, 2))
else
Result:= Result + '%' + IntToHex(L, 2);
end;
Result:= copy(Result, 1, Length(Result));
end;
delphi提供两个函数 httpencode()和httpdecode(),经过研究,它和escape()和unescape()差别很大。一个是字符集的问题,还有一个是httpencode()转换了许多escape()不转换的字符。比如:'/'等。
经过1天的尝试,由于屡试不成功,中间都想放弃了尝试了,但是最后终于找出了一个方法。下面贴出我的源代码,以供大家参考,在遇到这个问题时,就不用费尽精力查资料和尝试了:Function TYoungCHDisp.HtmlEncrypt(FilePath:String): Boolean;
var
I: integer;
Tmp1,Tmp2: TStringList;
S: String;
begin
Tmp1 := TStringList.Create;
Tmp2 := TStringList.Create;
If Not FileExists(FilePath) Then Exit;
Tmp1.LoadFromFile(FilePath);
S := '';
For I := 0 to Tmp1.Count - 1 do
begin
S := S + Encode(Tmp1.Strings[I]) + '%0D%0A';
end;
S := 'document.write(unescape("' + S + '"));';
Tmp2.Add('<script>');
Tmp2.Add('<!--');
Tmp2.Add(S);
Tmp2.Add('//-->');
Tmp2.Add('</script>');
Tmp2.SaveToFile(FilePath);
Tmp1.Free;
Tmp2.Free;
end;Function TYoungCHDisp.Encode(S: String): String;
const
NoConversion = ['A'..'Z','a'..'z','0'..'9','/'];
var
T: WideString;
I: Integer;
H,L: Byte;
begin
Result := '';
T := WideString(S);
For I := 1 to Length(T) Do
begin
if (Char(T[i]) in NoConversion) and (Ord(T[I]) < 127) Then
begin
Result := Result + T[I];
Continue;
end;
H := Hi(Ord(T[I]));
L := Lo(Ord(T[I]));
If Ord(T[I]) > 127 Then
Result := Result + '%u' + (IntToHex(H, 2) + IntToHex(L, 2))
else
Result:= Result + '%' + IntToHex(L, 2);
end;
Result:= copy(Result, 1, Length(Result));
end;
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货