Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does StrToInt('X5') returns 5 in Delphi?

Tags:

delphi

Why does StrToInt('X5') returns 5 in Delphi? Is X some scientific notation or something like it? Are there some other chars which will be converted to Integer as well?

like image 869
Fabio Gomes Avatar asked May 19 '09 16:05

Fabio Gomes


3 Answers

Not knowing Delphi, I'd wager that the "X" causes the function to assume the value is hexidecimal. Since 0x5 == 5, it appears to be working. Try X10 and see if you get back 16.

like image 125
Pesto Avatar answered Oct 02 '22 12:10

Pesto


In Delphi, hexadecimal values are marked with $ prefix:

a := $10;  // => a = 16

But since in some other languages (e.g. C) X is used for marking hexadecimal values, StrToInt function supports both $ and X prefixes, so both of the codes below return 16:

a := StrToInt('x10'); // => a = 16

a := StrToInt('$10'); // => a = 16
like image 20
vcldeveloper Avatar answered Oct 02 '22 12:10

vcldeveloper


It's hex notation. Try XF to see it return 15.

like image 22
mmx Avatar answered Oct 02 '22 11:10

mmx