Raised This Month: $12 Target: $400
 3% 

char_to_num() ?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
draft
Senior Member
Join Date: Jul 2007
Location: Russia, Saint-Petersburg
Old 10-07-2008 , 08:22   char_to_num() ?
Reply With Quote #1

simple question:
we have str_to_num(str[]) func
i need to get the first char of that string (str[0]) and convert it to num. If i put
str_to_num(str[0]) the compiler will think that it is double array (str[][]) but not single char, am i right?
I dont know is it available to use char_to_num(char), so need simple help how to solve the problem
thx in advance
__________________

Last edited by draft; 10-07-2008 at 08:29.
draft is offline
Greenberet
AMX Mod X Beta Tester
Join Date: Apr 2004
Location: Vienna
Old 10-07-2008 , 08:36   Re: char_to_num() ?
Reply With Quote #2

(str[0] - '0') == your number ( if the char was a number )
__________________
Greenberet is offline
Send a message via ICQ to Greenberet Send a message via MSN to Greenberet
SchlumPF*
Veteran Member
Join Date: Mar 2007
Old 10-07-2008 , 08:37   Re: char_to_num() ?
Reply With Quote #3

should work ;D

PHP Code:
new temp[1];
copytemp0somestring );

new 
num str_to_numtemp ); 
__________________
SchlumPF* is offline
Send a message via ICQ to SchlumPF*
[X]-RayCat
Senior Member
Join Date: Sep 2006
Old 10-07-2008 , 10:09   Re: char_to_num() ?
Reply With Quote #4

As far as i know this should be in scripting help?
[X]-RayCat is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 10-07-2008 , 11:18   Re: char_to_num() ?
Reply With Quote #5

Try this :

PHP Code:
first_char_to_num(szString[])
{
    if( 
isdigit(szString[0]) )
    {
        
szString[1] = 0
        
return str_to_num(szString)
    }
    return -
1

__________________
- tired and retired -

- my plugins -

Last edited by ConnorMcLeod; 10-07-2008 at 11:20.
ConnorMcLeod is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 10-07-2008 , 11:25   Re: char_to_num() ?
Reply With Quote #6

Quote:
Originally Posted by connorr View Post
Try this :

PHP Code:
first_char_to_num(szString[])
{
    if( 
isdigit(szString[0]) )
    {
        
szString[1] = 0
        
return str_to_num(szString)
    }
    return -
1

PHP Code:
first_char_to_num(const string[])
{
    static 
temp[2];
    
temp[0] = string[0];
    
// temp[1] will always stay 0
    
    
return ('0' <= temp[0] <= '9') ? str_to_num(temp) : -1;

That way, the string won't be changed in case it is used later.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Alka
AMX Mod X Plugin Approver
Join Date: Dec 2006
Location: malloc(null)
Old 10-07-2008 , 11:45   Re: char_to_num() ?
Reply With Quote #7

@Exolent - Little optimization.
PHP Code:
('0' <= temp[0] <= '9'
->
PHP Code:
isdigit(temp[0]) 
heh
Alka is offline
Prajch
Senior Member
Join Date: Dec 2007
Location: anger++
Old 10-07-2008 , 12:15   Re: char_to_num() ?
Reply With Quote #8

Quote:
Originally Posted by draft View Post
If i put
str_to_num(str[0]) the compiler will think that it is double array (str[][]) but not single char, am i right?
I'm pretty sure that sending an indexed character array to the function "slices" the string, such that the string the function receives is indexed starting from whatever index was used. For example, if you pass str[5] to the function, when the function accesses the array internally, str[0] will be (the original) str[5], str[1] will be (the original) str[6], and so on. The length of the string it accesses will therefore be the length of the original string minus the index used.

You can see this as the function being sent the address where the character is stored, and it starts reading the string from this address. If the function was expecting a regular value instead of an array, I think it could accept the indexed string parameter as a character instead.
Prajch is offline
Lee
AlliedModders Donor
Join Date: Feb 2006
Old 10-07-2008 , 12:44   Re: char_to_num() ?
Reply With Quote #9

Alka, I have as much evidence to support my claim as I assume you do, but I believe Exolent's version is faster. Calling natives is not free. I also think this is a better approach.

Code:
stock charToInt(char) {     if(!('0' <= char <= '9'))         return -1     return char - 48; }
__________________
No support via PM.

Last edited by Lee; 10-07-2008 at 14:32.
Lee is offline
Emp`
AMX Mod X Plugin Approver
Join Date: Aug 2005
Location: Decapod 10
Old 10-07-2008 , 12:47   Re: char_to_num() ?
Reply With Quote #10

Code:
 stock char_to_num( character )
 {
	switch( character )
	{
		case '1': return 1;
		case '2': return 2;
		case '3': return 3;
		case '4': return 4;
		case '5': return 5;
		case '6': return 6;
		case '7': return 7;
		case '8': return 8;
		case '9': return 9;
		case '0': return 0;
	}
	return -1;
 }
?
Emp` is offline
Send a message via AIM to Emp` Send a message via MSN to Emp` Send a message via Yahoo to Emp` Send a message via Skype™ to Emp`
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 05:18.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode