Recent

Author Topic: IntToStr bring me to lost hairs  (Read 717 times)

paule32

  • Hero Member
  • *****
  • Posts: 514
  • One in all. But, not all in one.
IntToStr bring me to lost hairs
« on: May 23, 2025, 04:41:25 pm »
I have this Function:

Code: Pascal  [Select][+][-]
  1. function IntToStr32(AValue: Integer): PChar; stdcall; export;
  2. var
  3.   temp   : PChar;
  4. begin
  5.   temp := StrAlloc(64);
  6.   _itoa(AValue, temp, 10);
  7.   if AValue = 123 then
  8.   writeln('temp: ' + temp);
  9.   Exit(temp);
  10. end;

when I call it with IntToStr32(123) I get no writeln

the Function _itioa is:

Code: Pascal  [Select][+][-]
  1. function _itoa  (Value: Integer; Buffer: PChar; Radix: Integer): PChar; cdecl; external 'msvcrt.dll' name '_itoa';

But the following Code works:

Code: Pascal  [Select][+][-]
  1. function IntToStr32(AValue: Integer): PChar; stdcall; export;
  2. var
  3.   temp   : PChar;
  4. begin
  5.   AValue := 123;
  6.   temp := StrAlloc(64);
  7.   _itoa(AValue, temp, 10);
  8.   if AValue = 123 then
  9.   writeln('temp: ' + temp);
  10.   Exit(temp);
  11. end;
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12256
  • FPC developer.
Re: IntToStr bring me to lost hairs
« Reply #1 on: May 23, 2025, 04:45:19 pm »
_itoa has a return value. This is so you know how many characters you have written so that you can provide proper zero terminating. 

paule32

  • Hero Member
  • *****
  • Posts: 514
  • One in all. But, not all in one.
Re: IntToStr bring me to lost hairs
« Reply #2 on: May 23, 2025, 04:50:57 pm »
when I do:

temp := _itoa(AValue, temp, 10);

or:

StrCopy(temp, _itoa(AValue, temp, 10));

I get same results:
temp: 20971088

But not 123.
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

Thaddy

  • Hero Member
  • *****
  • Posts: 17106
  • Ceterum censeo Trump esse delendam
Re: IntToStr bring me to lost hairs
« Reply #3 on: May 23, 2025, 05:00:39 pm »
If you see a pointer value why do you stop thinking about what the pointer points to?

Also,

Why do you use C libraries for that?
The Pascal compiler handles that just as efficient.

If I wasn't already certified mad, you will make me even more mad with such questions where you do not do your own research.

Funny you focus on 32 Intel Windows, which is not obtainable from MS anymore.
« Last Edit: May 23, 2025, 05:13:53 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

paule32

  • Hero Member
  • *****
  • Posts: 514
  • One in all. But, not all in one.
Re: IntToStr bring me to lost hairs
« Reply #4 on: May 23, 2025, 05:13:14 pm »
good news:
I have found a Solution:

Code: Pascal  [Select][+][-]
  1. function IntToStr32(Value: Integer): string; stdcall; export;
  2. var
  3.   Buffer: array[0..31] of Char;
  4.   Temp: PChar;
  5.   Negative: Boolean;
  6.   Digit: Integer;
  7.   Len: Integer;
  8. begin
  9.   Len := 0;
  10.   Temp := @Buffer[0];
  11.   Buffer[0] := #0;
  12.  
  13.   Negative := Value < 0;
  14.   if Negative then
  15.     Value := -Value;
  16.  
  17.   repeat
  18.     Digit := Value mod 10;
  19.     Move(Buffer[0], Buffer[1], Len + 1);
  20.     Buffer[0] := Chr(Ord('0') + Digit);
  21.     Inc(Len);
  22.     Value := Value div 10;
  23.   until Value = 0;
  24.  
  25.   if Negative then
  26.   begin
  27.     Move(Buffer[0], Buffer[1], Len + 1);
  28.     Buffer[0] := '-';
  29.     Inc(Len);
  30.   end;
  31.  
  32.   SetString(Result, Buffer, Len);
  33. end;
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

Thaddy

  • Hero Member
  • *****
  • Posts: 17106
  • Ceterum censeo Trump esse delendam
Re: IntToStr bring me to lost hairs
« Reply #5 on: May 23, 2025, 05:15:02 pm »
 ;) ;D
Yup, the setstring trick. (it is not a trick!)

One day you will learn.

 :P :-X

Your code is still highly inefficient, though: you should use val(), which is also used by inttostr.
Then again: we discussed that.... and you did not understand that.

This part is exceptionally bad, what are you trying there?
Code: Pascal  [Select][+][-]
  1.   repeat
  2.     Digit := Value mod 10;
  3.     Move(Buffer[0], Buffer[1], Len + 1);
  4.     Buffer[0] := Chr(Ord('0') + Digit);
  5.     Inc(Len);
  6.     Value := Value div 10;
  7.   until Value = 0;

Makes no sense.
« Last Edit: May 23, 2025, 05:28:40 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

paule32

  • Hero Member
  • *****
  • Posts: 514
  • One in all. But, not all in one.
Re: IntToStr bring me to lost hairs
« Reply #6 on: May 23, 2025, 06:48:49 pm »
it is not the efficency that I looking for.
I looking for workable...
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

 

TinyPortal © 2005-2018