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;
复制代码

 

posted on   疯狂delphi  阅读(627)  评论(0编辑  收藏  举报

编辑推荐:
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
阅读排行:
· 官方的 MCP C# SDK:csharp-sdk
· 一款 .NET 开源、功能强大的远程连接管理工具,支持 RDP、VNC、SSH 等多种主流协议!
· 提示词工程师自白:我如何用一个技巧解放自己的生产力
· 一文搞懂MCP协议与Function Call的区别
· 如何不购买域名在云服务器上搭建HTTPS服务

导航

统计

点击右上角即可分享
微信分享提示