Convert from Integer to String (Base Ten only) lazarus / 62-General ---------------------------------------------------------------------- WackaMold · 2021-05-02 22:09 This function makes the EXE 1K bytes larger than just calling Str(). It started as an alternative to itoa() "Integer To Ascii". Not yet tested with zero, negative values, nor large values. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function StrReverse( strVal : string ) : string; // return the string with characters in reverse ordervar  strRtn : string = ''; // function result  idxStr : integer    ; // indexer  chrCur : char       ; // current characterbegin  for idxStr := Byte(Length(strVal)) DownTo 1 do begin // scan all characters in the string in reverse order  // Byte() conversion avoids Warning: (4048) Type size mismatch, possible loss of data / range check error    chrCur := strVal[idxStr];                  // get current character    strRtn := strRtn + chrCur;                 // append it to the output  end;                                         // done scanning  StrReverse := strRtn;                        // pass result back to callerend; // StrReverse function StrFrmInt( prmInt : integer ) : string; // convert from Integer to String, in base-ten onlyvar  strRtn : string = ''          ; // function result ptrTmp : byte                 ; // value of current integer-digit  chrTmp : char                 ; // char  of current string -digitbegin  while prmInt > 0 do begin       // eat all of caller's integer value, from ones-place to largest-place    ptrTmp :=      prmInt Mod 10; // get value of current last digit    ptrTmp :=      ptrTmp  +  48; // convert from zero-based to '0'-based    chrTmp := Chr( ptrTmp )     ; // convert from byte-digit to character-digit    strRtn := strRtn + chrTmp   ; // append character-digit to result    prmInt :=      prmInt Div 10; // drop last digit from caller's integer  end                           ; // done eating caller's integer value  strRtn := StrReverse( strRtn ); // convert our characters from [ones-place through largest-place] to [largest-place through ones-place]   Result := strRtn              ; // pass result back to callerend; // StrFrmInt ---------------------------------------------------------------------- mangakissa · 2021-05-03 09:18 So, what's the problem? ---------------------------------------------------------------------- marcov · 2021-05-03 09:34 In case you are an embedded user (*) where it might matter, string support is higher level than plain pointers and chars. Rewrite them using pchar might save you some bytes if you really need them. (*) and by embedded I mean anything with less than 256k effective storage for programs. RPI is not embedded by this definition. ---------------------------------------------------------------------- engkin · 2021-05-03 10:44 You don't need StrReverse function. Just change the order of this line: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     strRtn := strRtn + chrTmp;to Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     strRtn :=  chrTmp + strRtn; ---------------------------------------------------------------------- Bart · 2021-05-03 10:46 Your routine to reverse the string is a bit suboptimal. FreePascal has a ReverseString function in strutils unit. If you cannot (or do not want to) include that unit, simply copy the code from there. Alternatively (see also my website, which has some other "suboptimal" ones as well, but this one is OK): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function ReverseString(const S: String): String;var i,j: LongInt;begin  j := Length(S);  Setlength(Result,j);  i := 1;  while i <= j do //Actually this is faster than a for loop  begin    result[i] := S[j-i+1];    inc(i);  end;end; Why can't or won't you sue the Str() function? Bart ---------------------------------------------------------------------- alpine · 2021-05-03 11:44 Just a side question - why is it necessary to make a string ltr and then reverse? Is there any subtle reason I am not aware of? ---------------------------------------------------------------------- engkin · 2021-05-03 11:47 @Bart, Because of Unicode I wouldn't call it ReverseString,  @y.ivanov, I don't think so. See my previous post. ---------------------------------------------------------------------- alpine · 2021-05-03 11:56 @engkin My question is partially because your previous post, partially because I reviewed some C library sources for itoa() and I what I saw was that some implementations really do reversing. And so I suspected reason I'm unfamiliar with. ---------------------------------------------------------------------- engkin · 2021-05-03 12:05 In this case probably it is speed. ---------------------------------------------------------------------- WackaMold · 2021-05-03 18:36 QuoteYou don't need StrReverse(). Code: [Select][Del] strRtn := strRtn + chrTmp; // ap pend character- digit to result [Ins] strRtn := chrTmp + strRtn; // prepend character-digit to result Thank you, this saved another 1K. QuoteWhy not use Str()?On the off chance of shrinking the EXE further, without the extra effort of trying a function written in assember. Using StrUtils or SysUtils or untStrings added far too much unused code, making the EXE more than twice larger. Speed is not a concern in this case, but might be useful in other cases. ---------------------------------------------------------------------- winni · 2021-05-03 19:32 Hi! Str () is part of the system unit - so no additional code is added. As Bart said: use Str() Winni ---------------------------------------------------------------------- alpine · 2021-05-03 21:46 If you want a slight reduction you can consider using ShortString instead of String. Your function is incorrect for zero and negative. As markov suggested, you should use PChar if the code size is critical. Here is example implementation for base 10 (not reentrant). Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function StrFrmInt(prmInt: Integer): PChar;const  Buf: array[0..22] of Char = '';var  I, Sign: Integer;begin  if prmInt < 0 then    Sign := -1 else    Sign := 1;  Buf[High(Buf)] := #0;  I := Pred(High(Buf));  repeat    Buf[I] := Char(48 + prmInt mod 10 * Sign);    prmInt := prmInt div 10;    Dec(I);  until (prmInt = 0) or (I < 1);  if Sign < 0 then  begin    Buf[I] := '-';    Dec(I);  end;  Result := PChar(@Buf[Succ(I)]);end; ---------------------------------------------------------------------- WackaMold · 2021-05-09 12:36 Cool. This version ignores Sign and has comments. It's good for only non-negative values. Code: [Select]function StrFrmInt( intPrm : Integer ) : PChar; // convert from non-negative Integer to String, in base-ten only. Adds 2K const   bufStr : array[ 0..22 ] of Char = ''            ; // null-terminated string; eats 32 bytes var   idxBuf : Integer                                ; // indexer into string buffer begin   intPrm :=               Abs    ( intPrm        ); // convert negative values to positive   bufStr[         High( bufStr ) ] := #0          ; // put string- terminator in last position   idxBuf := Pred( High( bufStr ) )                ; // point to next- to-        last position   repeat                                            // process loop     bufStr[ idxBuf ] := Char( 48 + intPrm Mod 10 ); // shift ones- place from integer-digit to character-digit; put that in buffer's current position     intPrm :=             Integer( intPrm Div 10 ); // drop ones-place from caller's integer; typecast suppresses Warning 4048 'Type size mismatch, possible loss of data / range check error'     Dec( idxBuf )                                 ; // point to preceding buffer position   until( intPrm = 0 ) Or ( idxBuf < 1 )           ; // done when [caller's integer is eaten up] or [we have run out of positions to fill]   Result := PChar( @bufStr[ Succ( idxBuf ) ] )    ; // return a string-pointer to the character after the current buffer position end; // StrFrmInt