PDA

View Full Version : [Tutorial] [TUT] How works strval!


¤Adas¤
11/03/2010, 04:37 PM
Hello, guys. I am sure that you can use strval. :D
But how does it work?
I have made a little function to explain:

Functions needed to do this:


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:


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! ;)

Kurence
17/03/2010, 12:57 PM
Hmm is it just my feeling or this was already released... Ah yes! It is basic function in pawn! :mrgreen: good work

aircombat
17/03/2010, 12:58 PM
nice work!

¤Adas¤
17/03/2010, 12:58 PM
Thanks :D:D:D:D:D