SA-MP Forums Archive
[Tutorial] [TUT] How works strval! - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] [TUT] How works strval! (/showthread.php?tid=133222)



[TUT] How works strval! - ¤Adas¤ - 11.03.2010

Hello, guys. I am sure that you can use strval.
But how does it work?
I have made a little function to explain:

Functions needed to do this:

pawn Code:
stock bool:IsNumericChar(character) return character > 47 && character < 58;

stock GetCharsCount(const string[], ...)
{
new Count, num = numargs();
for(new n = 1; n<num; n++) for(new i; i<strlen(string); i++) if(getarg(n, 0) && string[i] == getarg(n, 0)) Count++;
return Count;
}

stock GetNumericCharsCount(const string[]) return GetCharsCount(string, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57);

stock bool:GetNumericPartOfString(const string[], output[], &index)
{
new lenght = strlen(string);
if(!lenght || !GetNumericCharsCount(string)) return false;
while(index < lenght && !IsNumericChar(string[index])) index++;
new offset = index;
while(index < lenght && IsNumericChar(string[index])) output[index - offset] = string[index], index++;
return true;
}
And here is the function itself:

pawn Code:
stock strval(const string[])
{
new bool:minus, value, index, multiplier = 1, output[25];
if(string[0] == 45) minus = true;                 // Checking, if there is a "-" character.
if(!minus && !IsNumericChar(string[0])) return 0;
if(minus && !IsNumericChar(string[1])) return 0;
GetNumericPartOfString(string, output, index);      // Getting the numeric string part.
for(new i; i<strlen(output) - 1; i++) multiplier *= 10;
for(new i; i<strlen(output); i++, multiplier /= 10) value += (output[i] - 48) * multiplier; // Making a number from it...
return minus ? -value : value; // Returning the final value. If there was "-" character at the begin of string, it will return -value.
}
So, that's it. When you want to see, how some other functions work, just PM me.

I hope, you like my tutorial. Please, leave stupid comments!


Re: [TUT] How works strval! - Kurence - 17.03.2010

Hmm is it just my feeling or this was already released... Ah yes! It is basic function in pawn! good work


Re: [TUT] How works strval! - aircombat - 17.03.2010

nice work!


Re: [TUT] How works strval! - ¤Adas¤ - 17.03.2010

Thanks