[Tutorial] [TUT] How works strval!
#1

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!
Reply
#2

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

nice work!
Reply
#4

Thanks
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)