zoukankan      html  css  js  c++  java
  • 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;
  • 相关阅读:
    开启sftp服务日志并限制sftp访问目录
    Django-16-安装前端项目
    sql
    Django-15-用户模块、认证授权、session会话认证和token认证
    Django-14-项目工程搭建
    开发小技巧
    Django-13-类视图设计原则
    Django-11-自动生成routers路由、自定义action
    Django-9-序列化器中各种校验方式
    Django—问题—生成迁移脚本时报错:You are trying to add a non-nullable field 'gender' to interfaces without a default
  • 原文地址:https://www.cnblogs.com/FKdelphi/p/12620494.html
Copyright © 2011-2022 走看看