Recent

Author Topic: [SOLVED] Bug in TryStrToInt??  (Read 3032 times)

bonmario

  • Sr. Member
  • ****
  • Posts: 346
[SOLVED] Bug in TryStrToInt??
« 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
« Last Edit: September 17, 2019, 01:53:01 pm by bonmario »

Handoko

  • Hero Member
  • *****
  • Posts: 5153
  • My goal: build my own game engine using Lazarus
Re: Bug in TryStrToInt??
« Reply #1 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

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.
« Last Edit: September 16, 2019, 11:08:32 am by Handoko »

avk

  • Hero Member
  • *****
  • Posts: 752
Re: Bug in TryStrToInt??
« Reply #2 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).

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Bug in TryStrToInt??
« Reply #3 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.
« Last Edit: September 16, 2019, 10:53:13 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Bug in TryStrToInt??
« Reply #4 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
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

avk

  • Hero Member
  • *****
  • Posts: 752
Re: Bug in TryStrToInt??
« Reply #5 on: September 16, 2019, 10:57:22 am »
Yes, and TryStrToInt returns True.

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Bug in TryStrToInt??
« Reply #6 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.
« Last Edit: September 16, 2019, 11:16:15 am by wp »

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Bug in TryStrToInt??
« Reply #7 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
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 14371
  • Sensorship about opinions does not belong here.
Re: Bug in TryStrToInt??
« Reply #8 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.

« Last Edit: September 16, 2019, 12:02:35 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11452
  • FPC developer.
Re: Bug in TryStrToInt??
« Reply #9 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.

Thaddy

  • Hero Member
  • *****
  • Posts: 14371
  • Sensorship about opinions does not belong here.
Re: Bug in TryStrToInt??
« Reply #10 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.

« Last Edit: September 16, 2019, 12:15:24 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

avk

  • Hero Member
  • *****
  • Posts: 752
Re: Bug in TryStrToInt??
« Reply #11 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. :)

Thaddy

  • Hero Member
  • *****
  • Posts: 14371
  • Sensorship about opinions does not belong here.
Re: Bug in TryStrToInt??
« Reply #12 on: September 16, 2019, 12:17:17 pm »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

avk

  • Hero Member
  • *****
  • Posts: 752
Re: Bug in TryStrToInt??
« Reply #13 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.

Thaddy

  • Hero Member
  • *****
  • Posts: 14371
  • Sensorship about opinions does not belong here.
Re: Bug in TryStrToInt??
« Reply #14 on: September 16, 2019, 12:27:20 pm »
Oh well, IntToStr causes it.....
This is simply not a bug... :P
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018