Delphi实现js中的escape()编码和unescape()解码
相关资料:
http://www.it588.cn/softdev/2019-05-06/603.html
方法一:
引入单元:
uses
ActiveX,
ComObj;
实例调用:
//javascript中的escape()函数的delphi实现
function Escape(s: string): string;
var
sc: OleVariant;
begin
ActiveX.CoInitialize(nil);
try
sc := CreateOleObject('MSScriptControl.ScriptControl.1');
sc.Language := 'javascript';
Result := sc.Eval('escape(''' + s + ''')');
except
Result := '';
end;
ActiveX.CoUninitialize;
end;
//javascript中的unescape()函数的delphi实现
function UnEscape(s: string): string;
var
sc: OleVariant;
begin
ActiveX.CoInitialize(nil);
try
sc := CreateOleObject('MSScriptControl.ScriptControl.1');
sc.Language := 'javascript';
s := StringReplace(s, '/u', '%u', [rfReplaceAll, rfIgnoreCase]); //'\u' 一些格式转换,根据自己需要处理
Result := sc.Eval('unescape(''' + s + ''')');
except
end;
ActiveX.CoUninitialize;
end;
方法二:
相关资料:
https://blog.csdn.net/cc001100/article/details/81015182
http://www.delphitop.com/html/zifuchuan/4728.html
http://www.tansoo.cn/?p=736
1 function AnsiToUnicode(Str: ansistring): string; 2 var 3 s: ansistring; 4 i: integer; 5 j, k: string[2]; 6 a: array [1..1000] of ansichar; 7 begin 8 s := ''; 9 StringToWideChar(Str, @(a[1]), 500); 10 i := 1; 11 while ((a[i] <> #0) or (a[i+1] <> #0)) do 12 begin 13 j := IntToHex(Integer(a[i]), 2); 14 k := IntToHex(Integer(a[i+1]), 2); 15 s := s + '\u'+ k + j; 16 i := i + 2; 17 end; 18 Result := s; 19 end; 20 21 function UnicodeToAnsi(aSubUnicode: string): string; 22 var 23 tmpLen, iCount: Integer; 24 tmpWS: WideString; 25 begin 26 tmpWS := ''; 27 iCount := 1; 28 tmpLen := Length(aSubUnicode); 29 while iCount <= tmpLen do 30 try 31 if (Copy(aSubUnicode, iCount, 1) = '\') 32 and (Copy(aSubUnicode, iCount, 2) = '\u') then 33 begin 34 tmpWS := tmpWS 35 + WideChar(StrToInt('$' + Copy(aSubUnicode, iCount + 2, 4))); 36 iCount := iCount + 6; 37 end 38 else 39 begin 40 tmpWS := tmpWS + Copy(aSubUnicode, iCount, 1); 41 iCount := iCount + 1; 42 end; 43 except 44 end; 45 Result := tmpWS; 46 end;
欢迎关注我,一起进步!扫描下方二维码即可加我