tags:

views:

519

answers:

5

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?

+1  A: 

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

Mehrdad Afshari
F is 15, not 16.
Pesto
+10  A: 

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.

Pesto
A: 

Probably hexadecimal notation.

The X indicates that the number that follows is hexadecimal (0-9 + A-F).

GvS
A: 

As the comments have stated, StrToInt does not understand octal.

To answer your other question, if there are other strings that return unlikely answers... Try this:

StrToInt('010')

If it returns 8, then that function also understands octal notation.

sharth
Nope, 010 is converted to 10, it just understands decimal and hexadecimal.
Fabio Gomes
$ - when leading should also indicate hexadecimal notation
Ben Schwehn
Marco van de Voort
+8  A: 

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
vcldeveloper