Recent

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

bonmario

  • Sr. Member
  • ****
  • Posts: 296
[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: 3166
  • 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

  • Full Member
  • ***
  • Posts: 133
    • my self-education project
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

  • Full Member
  • ***
  • Posts: 237
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 IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircrafts

Zvoni

  • Full Member
  • ***
  • Posts: 237
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 IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircrafts

avk

  • Full Member
  • ***
  • Posts: 133
    • my self-education project
Re: Bug in TryStrToInt??
« Reply #5 on: September 16, 2019, 10:57:22 am »
Yes, and TryStrToInt returns True.

wp

  • Hero Member
  • *****
  • Posts: 6235
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 »
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

Zvoni

  • Full Member
  • ***
  • Posts: 237
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 IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircrafts

Thaddy

  • Hero Member
  • *****
  • Posts: 8967
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 »
Most people that want to use threading should learn to patch their jeans first: use a needle.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7456
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: 8967
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 »
Most people that want to use threading should learn to patch their jeans first: use a needle.

avk

  • Full Member
  • ***
  • Posts: 133
    • my self-education project
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: 8967
Re: Bug in TryStrToInt??
« Reply #12 on: September 16, 2019, 12:17:17 pm »
Most people that want to use threading should learn to patch their jeans first: use a needle.

avk

  • Full Member
  • ***
  • Posts: 133
    • my self-education project
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: 8967
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
Most people that want to use threading should learn to patch their jeans first: use a needle.