整数到字符串强制转换,Delphi类型转换

时间:2018-02-18 18:38:00

标签: delphi casting

在Delphi中进行维护工作时遇到了一些我不太了解的代码。我写了一个简单的控制台应用程序,以帮助我思考这一点,但我仍然感到困惑。如果您查看附加的代码,您会看到我只在其中添加了1条评论。这是我的问题的位置。

我认为可能放入X的值可能是字符串的地址,或者字符串中每个(Unicode)字符的位值用于得到X中的值但是如果你运行的话程序多次在每次迭代时看起来都是随机的X值。

任何成年人的监督都将不胜感激,谢谢。

program OrdinalProject;
{$APPTYPE CONSOLE}
{$R *.res}
uses
  System.SysUtils;
var
  A : string;
  B : ^string;
  X : integer;
begin
  A := 'ABCD';
  WriteLn(Format('Content of A = %s',[A]));
  WriteLn(Format('Address of A = %p',[@A]));
  WriteLn('');

  B := @A;
  WriteLn(Format('Content of B = %p',[B]));
  WriteLn(Format('Address of B = %p',[@B]));
  WriteLn('');

  X := integer(A); // What is going on here and why doesn't this fail?
  WriteLn(Format('Content of X = %d',[X]));
  WriteLn(Format('Address of X = %p',[@X]));
  Readln;
end.

感谢您的回复!我现在明白了。

0 个答案:

没有答案