Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2013-02

StrToInt -- Strange behaviour

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Ewald

Hello,

Try running the following program and be amazed of the output:

    Program Test;
    Uses Sysutils;
    Begin
        WriteLn(StrToInt('x1'));
        WriteLn(StrToInt('x0'));
        WriteLn(StrToInt('xabcdef'));
    End.

Apparently the StrToInt function interprets strings starting with an `x` as hexadecimal numbers? This behaviour seems rather strange to me (strings starting with 0x would be a bit more understandable, but then again, we're programming pascal here, not C) and it is not mentioned in the documentation. What do I do with it?

A- File a bug concerning incomplete documentation? B- File a bug concerning incorrect behaviour of StrToInt()? C- Do nothing and live with it.
D- ....?

Additional information
Compiler Version: 2.6.0

Ewald

Reply by email

Marco van de Voort
to Ewald

In our previous episode, Ewald said:

Show quoted text 10 lines
Apparently the StrToInt function interprets strings starting with an `x` as hexadecimal numbers? This behaviour seems rather strange to me (strings starting with 0x would be a bit more understandable, but then again, we're programming pascal here, not C) and it is not mentioned in the documentation. What do I do with it? A- File a bug concerning incomplete documentation? B- File a bug concerning incorrect behaviour of StrToInt()? C- Do nothing and live with it. D- ....?

E- Test with Delphi and conclude it is compatibility.

... but a doc bugreport wouldn't be bad.

Reply by email

Sven Barth
to Ewald: Try running the following program and be amazed of the output: Program Test; Uses…
Ewald wrote 20 lines
Hello, Try running the following program and be amazed of the output: Program Test; Uses Sysutils; Begin WriteLn(StrToInt('x1')); WriteLn(StrToInt('x0')); WriteLn(StrToInt('xabcdef')); End. Apparently the StrToInt function interprets strings starting with an `x` as hexadecimal numbers? This behaviour seems rather strange to me (strings starting with 0x would be a bit more understandable, but then again, we're programming pascal here, not C) and it is not mentioned in the documentation. What do I do with it?

IntToStr simply calls through to Val and there is documented (more or less) that it supports binary (%), octal (&) and hexadecimal ($, 0x) values as well (see here:
http://www.freepascal.org/docs-html/rtl/system/val.html ). That it also supports 'x' and 'X' as a prefix for hexadecimal values is because of Delphi compatibility.

As a sidenote: the prefixes %, & and $ can be used in normal source code as well.

A- File a bug concerning incomplete documentation? B- File a bug concerning incorrect behaviour of StrToInt()? C- Do nothing and live with it. D- ....?

I'd say A. On the one hand a reference from StrToInt(64), StrToInt(64)Def and TryStrToInt(64) to Val should be added and on the other hand Val's documentation should be extended by the fact that '0x', 'X' and 'x' are valid prefixes for hexadecimal values as well. And while we're at it references from the 32bit StrToInt variants to the 64bit variants could be added as well (they do exist the other way round as one can see here:
http://www.freepascal.org/docs-html/rtl/sysutils/strtoint64.html ).

So if you would please be so kind to open a bug report :)

Regards,
Sven

Reply by email

Ewald
to Sven Barth

Once upon a time, Sven Barth said:

Show quoted text 17 lines
On 21.02.2013 21:01, Ewald wrote: > > Apparently the StrToInt function interprets strings starting with an `x` > as hexadecimal numbers? This behaviour seems rather strange to me > (strings starting with 0x would be a bit more understandable, but then > again, we're programming pascal here, not C) and it is not mentioned in > the documentation. What do I do with it? IntToStr simply calls through to Val and there is documented (more or less) that it supports binary (%), octal (&) and hexadecimal ($, 0x) values as well (see here: http://www.freepascal.org/docs-html/rtl/system/val.html ). That it also supports 'x' and 'X' as a prefix for hexadecimal values is because of Delphi compatibility. As a sidenote: the prefixes %, & and $ can be used in normal source code as well.

Of course, these I use, but the `xabcd` syntax cannot be used in source code to represent numbers. Hence my confusion about what to do with it.

Show quoted text 16 lines
> A- File a bug concerning incomplete documentation? > B- File a bug concerning incorrect behaviour of StrToInt()? > C- Do nothing and live with it. > D- ....? I'd say A. On the one hand a reference from StrToInt(64), StrToInt(64)Def and TryStrToInt(64) to Val should be added and on the other hand Val's documentation should be extended by the fact that '0x', 'X' and 'x' are valid prefixes for hexadecimal values as well. And while we're at it references from the 32bit StrToInt variants to the 64bit variants could be added as well (they do exist the other way round as one can see here: http://www.freepascal.org/docs-html/rtl/sysutils/strtoint64.html ). So if you would please be so kind to open a bug report :)

Here you go: http://bugs.freepascal.org/view.php?id=23933 :-)

Ewald

Reply by email

Ewald
to Marco van de Voort: In our previous episode, Ewald said: E- Test with Delphi and conclude it is compatibility.

Once upon a time, Marco van de Voort said:

Show quoted text 12 lines
In our previous episode, Ewald said: > Apparently the StrToInt function interprets strings starting with an `x` > as hexadecimal numbers? This behaviour seems rather strange to me > (strings starting with 0x would be a bit more understandable, but then > again, we're programming pascal here, not C) and it is not mentioned in > the documentation. What do I do with it? > > A- File a bug concerning incomplete documentation? > B- File a bug concerning incorrect behaviour of StrToInt()? > C- Do nothing and live with it. > D- ....? E- Test with Delphi and conclude it is compatibility.

So there was another option... Totally forgot about delphi compatibility ;-)

Ewald

Reply by email