一个ScriptControl组件用来解码的例子,主要是为了完成\uXXXX方式的汉字转换.
unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, OleCtrls, MSScriptControl_TLB;
type TForm1 = class(TForm) Memo1: TMemo; Button1: TButton; Edit1: TEdit; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;
var Form1: TForm1;
implementation
{$R *.dfm}
function Encode2(s:WideString):String; var i,len:Integer; cur:Integer; t:String; begin Result:= ''; len:=Length(s); i:=1; while i <=len do begin cur:=ord(s[i]); //BCD转换 if ( cur<19968) or (cur>40869) then //单字不转 begin Result:=Result+s[i]; end else begin FmtStr(t, '\u%4.4X',[cur]); Result:=Result+t; end; inc(i); end; end;
procedure TForm1.Button1Click(Sender: TObject); var s,str:string; i:integer; vScript: TScriptControl; begin
s:=Encode2(edit1.Text); memo1.Lines.Add(s); vScript:=TScriptControl.Create(nil); vScript.Language := 'JavaScript'; str:=vScript.Eval('unescape('''+s+''')'); memo1.Lines.Add(str); vScript.free; end;
end. |