Lazarus

Programming => General => Topic started by: bonmario on September 16, 2019, 10:17:23 am

Title: [SOLVED] Bug in TryStrToInt??
Post by: bonmario on September 16, 2019, 10:17:23 am
Hi,
view this code:
Code: Pascal  [Select][+][-]
  1. var WrkNum:Integer;
  2. begin
  3.   WrkNum:=99;
  4.   TryStrToInt('xd', WrkNum);
  5.   ShowMessage(IntToStr(WrkNum));
  6. end;
  7.  

At the end of procedure, the value of "WrkNum" is "13".

It's a bug or i'm wrong?

Thanks in advance. Mario
Title: Re: Bug in TryStrToInt??
Post by: Handoko on September 16, 2019, 10:38:21 am
Sorry I was wrong. Please ignore this post.

No, it is not a bug.

The documentation does not say it will return any value for the out parameter i if the conversion failed:
https://www.freepascal.org/docs-html/rtl/sysutils/trystrtoint.html (https://www.freepascal.org/docs-html/rtl/sysutils/trystrtoint.html)

You may expect to get 0, but no ... some may expect -1 if the function fails. So it is 'better' to not provide any expected value if it fails.
Title: Re: Bug in TryStrToInt??
Post by: avk on September 16, 2019, 10:45:47 am
Everything seems to be somewhat more interesting. d is the hexadecimal notation of 13. Try TryStrToInt ('x101', WrkNum).
Title: Re: Bug in TryStrToInt??
Post by: Zvoni on September 16, 2019, 10:49:27 am
Nevermind that TryStrToInt will return False if the conversion fails.
So why not just check the return-value of TryStrToInt?
If True (Conversion successful) use your Variable WrkNum, if False (Conversion failed), discard ANY value returned in WrkNum.

EDIT: Damn. I just noticed, that TryStrToInt returns True for the example.
Title: Re: Bug in TryStrToInt??
Post by: Zvoni on September 16, 2019, 10:55:13 am
Everything seems to be somewhat more interesting. d is the hexadecimal notation of 13. Try TryStrToInt ('x101', WrkNum).
'x101' returns 257 for me
Title: Re: Bug in TryStrToInt??
Post by: avk on September 16, 2019, 10:57:22 am
Yes, and TryStrToInt returns True.
Title: Re: Bug in TryStrToInt??
Post by: wp on September 16, 2019, 10:57:57 am
Well, in this case the conversion will even be successful as avk noted. Allowing hex numbers in C notation in Pascal is a bit surprising (instead of preceeding '$'), and allowing to start them with "x" instead of "0x" even more (at least to me as a non-C programmer). Is "xd" a valid C hex number? (or must it be "0xd"?) If not we have a bug here.

[EDIT]
Delphi accepts 'xd' as valid integer, too, and returns 13 like FPC.
Title: Re: Bug in TryStrToInt??
Post by: Zvoni on September 16, 2019, 11:00:03 am
Found it:
TryStrToInt uses the Val-Function
https://www.freepascal.org/docs-html/rtl/system/val.html
Quote
For hexadecimal values, the prefix '0x' or 'x' (case insensitive) may be used as well.
There it is: Your 'xd' is interpreted as a hexadecimal number and 'd' is a valid Hex-Symbol
Title: Re: Bug in TryStrToInt??
Post by: Thaddy on September 16, 2019, 11:43:21 am
Morons: 99 is already a number and not a string!!! And so the result is false. Programmer error....
Code: Pascal  [Select][+][-]
  1. program untitled;
  2. {$mode delphi}{$H+}
  3. uses sysutils;
  4. var WrkNum:Integer;
  5. begin
  6.   WrkNum:=99;
  7.   if TryStrToInt('99', WrkNum) then
  8.     writeln(IntToStr(WrkNum));
  9. end.

Title: Re: Bug in TryStrToInt??
Post by: marcov on September 16, 2019, 11:47:59 am
Morons: 99 is already a number and not a string!!! And so the result is false. Programmer error....

So now read it again. That value is just to intialize the var in a test to show it is changed.
Title: Re: Bug in TryStrToInt??
Post by: Thaddy on September 16, 2019, 12:03:28 pm
Well. read it again, Marco... Adapted typo..
TryStrToInt has no formatting, like Format(). Xd needs val() or the likes.
Probably means:
Code: Pascal  [Select][+][-]
  1. {$mode delphi}{$H+}
  2. uses sysutils;
  3. var WrkNum:Integer;
  4. begin
  5.   WrkNum:=99;
  6.   writeln(format('$%X', [WrkNum]));
  7. end.

Title: Re: Bug in TryStrToInt??
Post by: avk on September 16, 2019, 12:08:20 pm
There it is: Your 'xd' is interpreted as a hexadecimal number and 'd' is a valid Hex-Symbol
Well, yes, Cap said: reading documentation is always helpful. :)
Title: Re: Bug in TryStrToInt??
Post by: Thaddy on September 16, 2019, 12:17:17 pm
This one? https://www.freepascal.org/docs-html/rtl/sysutils/trystrtoint.html
See above.
Title: Re: Bug in TryStrToInt??
Post by: avk on September 16, 2019, 12:23:53 pm
Code: Pascal  [Select][+][-]
  1. function TryStrToInt(const s: string; out i : Longint) : boolean;
  2. var Error : word;
  3. begin
  4.   Val(s, i, Error);
  5.   TryStrToInt:=Error=0
  6. end;  
  7.  
therefore it is useful to also see Val documentation.
Title: Re: Bug in TryStrToInt??
Post by: Thaddy on September 16, 2019, 12:27:20 pm
Oh well, IntToStr causes it.....
This is simply not a bug... :P
Title: Re: Bug in TryStrToInt??
Post by: avk on September 16, 2019, 12:49:33 pm
Did I somewhere said that this is a bug?
Title: Re: Bug in TryStrToInt??
Post by: PascalDragon on September 17, 2019, 09:20:24 am
Morons: 99 is already a number and not a string!!! And so the result is false. Programmer error....
As marcov said, it's not a programmer error (except maybe not checking the result of TryStrToInt). bonmario simply set WrkNum to some defined value to show that TryStrToInt changes that variable due to the successful conversion of xd to 13.
Title: Re: Bug in TryStrToInt??
Post by: bonmario on September 17, 2019, 01:52:45 pm
Thanks to all, solved using "character.IsNumber" before.

Hi, Mario
TinyPortal © 2005-2018