Delphi的Format函数大家都用得很多,第二个参数用着确实很方便。最近在数据库开发应用中需要自己创建一个带array of const参数的函数,对于常用的类型String,Integer,Pointer处理都没什么问题,但当用到Widestring类型时却出错,摸索了一上午,感觉获益良多。现在将问题、解决问题的思路、分析方法等一一道来,希望对诸君有所启发就达到了我写这篇文章的目的了!
Delphi中array of const应用///环境:Winxp + D7
Delphi中array of const应用
///进入D7,在默认的新建工程中增加一过程Test(m: Array of const);
Delphi中array of const应用
procedure TForm1.test(m: array of const);
Delphi中array of const应用
var
Delphi中array of const应用i, zero: Integer;
Delphi中array of const应用s, t: String;
Delphi中array of const应用c: Char;
Delphi中array of const应用
const
Delphi中array of const应用sBoolean: Array [Boolean] 
of string = ('False''True');
Delphi中array of const应用
begin
Delphi中array of const应用s :
= '';
Delphi中array of const应用
for i := 0 to High(m) do with m[i] do
Delphi中array of const应用    
case VType of //写到这,按住Ctrl点击VType,打开System单元,将VType的枚举值贴到Case语句
Delphi中array of const应用      vtInteger:    (VInteger: Integer; VType: Byte);
Delphi中array of const应用      vtBoolean:    (VBoolean: Boolean);
Delphi中array of const应用      vtChar:       (VChar: Char);
Delphi中array of const应用      vtExtended:   (VExtended: PExtended);
Delphi中array of const应用      vtString:     (VString: PShortString);
Delphi中array of const应用      vtPointer:    (VPointer: Pointer);
Delphi中array of const应用      vtPChar:      (VPChar: PChar);
Delphi中array of const应用      vtObject:     (VObject: TObject);
Delphi中array of const应用      vtClass:      (VClass: TClass);
Delphi中array of const应用      vtWideChar:   (VWideChar: WideChar);
Delphi中array of const应用      vtPWideChar: (VPWideChar: PWideChar);
Delphi中array of const应用      vtAnsiString: (VAnsiString: Pointer);
Delphi中array of const应用      vtCurrency:   (VCurrency: PCurrency);
Delphi中array of const应用      vtVariant:    (VVariant: PVariant);
Delphi中array of const应用      vtInterface: (VInterface: Pointer);
Delphi中array of const应用      vtWideString: (VWideString: Pointer);
Delphi中array of const应用      vtInt64:      (VInt64: PInt64);
Delphi中array of const应用    
end;
Delphi中array of const应用Delete(s, 
11);
Delphi中array of const应用Self.Caption :
= s;
Delphi中array of const应用
end;
Delphi中array of const应用
///继续写,对各枚举值进行处理!这里作一下解释,Array of const正是由TVarRec类型组成的!
Delphi中array of const应用
///请看Case of语句中的代码:
Delphi中array of const应用      vtInteger: s :
= s + ';' + IntToStr(VInteger);
Delphi中array of const应用      vtBoolean: s :
= s + ';' + sBoolean[VBoolean];
Delphi中array of const应用      vtChar: s :
= s + ';' + VChar;
Delphi中array of const应用      vtExtended: s :
= s + ';' + FloatToStr(VExtended^);
Delphi中array of const应用      vtString:
Delphi中array of const应用        
if Assigned(VString) then begin
Delphi中array of const应用          t :
= VString^;
Delphi中array of const应用          s :
= s + ';' + t;
Delphi中array of const应用        
end;
Delphi中array of const应用      vtPointer:
Delphi中array of const应用        
if Assigned(VPointer) then
Delphi中array of const应用          s :
= Format('%S; Pointer: $%X ',[s, Integer(VPointer)]);
Delphi中array of const应用      vtPChar:
Delphi中array of const应用        
if Assigned(VPChar) then begin
Delphi中array of const应用          t :
= VPChar^;
Delphi中array of const应用          s :
= s + ';' + t;
Delphi中array of const应用        
end;
Delphi中array of const应用      vtObject:
Delphi中array of const应用        
if Assigned(VObject) then
Delphi中array of const应用          s :
= Format('%S; $%X ClassName: %S ',[s, Integer(@VObject), VObject.ClassName]);
Delphi中array of const应用      vtClass:
Delphi中array of const应用        
if Assigned(VClass) then
Delphi中array of const应用          s :
= Format('%S; Class Reference $%X - ClassName: %S ',[s, Integer(VClass), VClass.ClassName]);
Delphi中array of const应用      vtWideChar:
Delphi中array of const应用        
begin
Delphi中array of const应用          t :
= VWideChar;
Delphi中array of const应用          s :
= s + ';' + t;
Delphi中array of const应用        
end;
Delphi中array of const应用      vtPWideChar:
Delphi中array of const应用        
if Assigned(VPWideChar) then begin
Delphi中array of const应用          t :
= VPWideChar^;
Delphi中array of const应用          s :
= s + ';' + t;
Delphi中array of const应用        
end;
Delphi中array of const应用      vtAnsiString:
Delphi中array of const应用        
if Assigned(VAnsiString) then begin
Delphi中array of const应用          t :
= PChar(VAnsiString);
Delphi中array of const应用          s :
= s + ';' + t;
Delphi中array of const应用        
end;
Delphi中array of const应用      vtCurrency:
Delphi中array of const应用        
if Assigned(VCurrency) then
Delphi中array of const应用          s :
= s + ';' + FloatToStr(VCurrency^);
Delphi中array of const应用      vtVariant:
Delphi中array of const应用        
if Assigned(VVariant) then
Delphi中array of const应用          s :
= s + '; This is variant ';
Delphi中array of const应用      vtInterface:
Delphi中array of const应用        
if Assigned(VInterface) then
Delphi中array of const应用          s :
= Format('%S; Interface: $%X',[s, Integer(VInterface)]);
Delphi中array of const应用      vtWideString:
Delphi中array of const应用        
if Assigned(VWideString) then begin
Delphi中array of const应用          t :
= PWideString(VWideString)^;
Delphi中array of const应用          s :
= s + ';' + t;
Delphi中array of const应用        
end;
Delphi中array of const应用      vtInt64:
Delphi中array of const应用        
if Assigned(VInt64) then
Delphi中array of const应用          s :
= s + ';' + IntToStr(VInt64^);
Delphi中array of const应用

加上一按钮测试该函数
Delphi中array of const应用procedure TForm1.Button1Click(Sender: TObject);
Delphi中array of const应用
var
Delphi中array of const应用    ws: WideString;
Delphi中array of const应用
begin
Delphi中array of const应用    ws :
= 'dda这是一个测试dfa';
Delphi中array of const应用    test([self, 
'sdf'2.3324, ws, TForm]);
Delphi中array of const应用
end;
Delphi中array of const应用

可以看到测试结果,变量ws的值没有显示出来,怎么办呢?

我们可以看到WideString类型的值是指针,我们就从这里着手,在事件中添加一句:
Button1.Caption := Format('$%X',[Integer(@ws)]);
此句的作用是显示出ws的地址
再在Test函数中也加上类似的语句,并注释掉无用的语句:
//t := PWideString(VWideString)^;
//s := s + ';' + t;
s := s + ';' + Format('$%X',[Integer(VWideString)]);
运行可看到二个地址不一样,说明Delphi对传入的参数数据作了复制
因此将其强制转换成PWidechar应该可以,增加一变量声明
w: WideString;

w := PWideString(VWideString)^;
s := s + ';' + w;
但运行结果却只显示一个字符,别沮丧,已经摸到门道了!

我们知道Format可以处理Widestring类型,这里只得到一个字符,说明字符被截断了。Delphi中的字符串是以#0结束,Widestring以二个#0结束,可以肯定w := PWideString(VWideString)^这句Delphi作转换时肯定将其默认作为AnsiString处理了。分析到这里已经可动手写下去了.....

p: PByte;

        if Assigned(VWideString) then begin
          t := '';
          zero := 0;
          p := VWideString;
          repeat
            c := char(p^);
            inc(p);
            if c = #0 then
              inc(zero) else
              begin
                zero := 0;
                t := t + c;
              end;
          until zero = 2;
          s := s + ';' + t;
        end;

但是显示汉字却变成乱码了,而且处理也显得臃肿。到这里我们已经明白了,VWideString所指示的字符串是二字节宽字符串,而且Intel的字节顺序也是低位在前,高位在后。因此可用PWord进行处理!

删除c,zero,w变量,p改成:
p: PWord;

        if Assigned(VWideString) then begin
          t := '';
          p := VWideString;
          repeat
            t := t + widechar(p^);
            inc(p);
          until p^ = 0;
          s := s + ';' + t;
        end;
可以看到核心代码已经很精练了,运行已经显示正常,汉字也无乱码了!至此我们似乎是大功告成了,但静下来想想,Delphi支持WideString到String的转换,它也应该有这样的处理代码。
而且在循环中t := t + widechar(p^);语句处下一断点,运行到断点处,再打开CPU窗口,看到看似简洁的代码,单此一句,编译器都要给它加上一大堆处理代码。找到系统的字符串处理函数很有必要,经过在System.pas单元中搜索WideString,找到函数:procedure WideCharToStrVar(Source: PWideChar; var Dest: string);
呵呵,这正是我们要的!!!
现在循环语句及P变量都可删除了,代码我就省略了。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-10
  • 2022-12-23
  • 2022-01-30
  • 2022-12-23
  • 2021-11-02
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-06-06
  • 2021-11-03
相关资源
相似解决方案