开发者

C-style hexadecimals in Delphi - undocumented feature?

I noticed by chance that the following code

var
  I: Integer;

begin
  I:= StrToInt('0xAA');
  ShowMessage(IntToStr(I)); 开发者_运维技巧 // shows 170 = $AA
end;

is OK in Delphi 2009. BTW the feature helped me to extract hexadecimal constants from C header file.

I wonder is it OK to use the feature or the feature is about to be "fixed" in future versions?


It's a feature, and you can rely on it. One of the philosophical changes that occurred in the evolution of Turbo Pascal into Delphi was the acknowledgment that Delphi lives in a C-dominated world and there was more to be gained by gracefully accepting or tolerating C-isms than ignoring them and forcing the Delphi developer to sort it out. Interop with C++ Builder as mentioned by Rob was a factor, but so was the fact that Delphi was designed first for Windows, and Windows has a lot of C language artifacts in the Windows API.

I think the term "impedance mismatch" may apply here - it was simple enough to remove the impedance mismatch between Delphi hex handling and "Rest of World", so we did.


Recall that the Delphi RTL is used by C++ Builder, too. The documentation doesn't go into detail about exactly what it means when it says StrToIntaccepts "decimal or hexadecimal notation." You can safely expect StrToInt to continue to accept C-style numbers.

Val accepts the same input, as does Read (because they all end up calling System._ValLong).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜