Computing.Net > Forums > Programming > re: converting char array into int

re: converting char array into int

Reply to Message Icon

Original Message
Name: mydreams84
Date: September 26, 2004 at 08:02:38 Pacific
Subject: re: converting char array into int
OS: unix
CPU/Ram: don't know
Comment:

Hi there,

I am a Java programmer transitioning into a C++ programmer and I'm still new at all of this. Even for Java I'm still new at it. My problem is that if I read in a character array of numbers as well as letters, how can i pick out ONLY the numbers from the character array and then convert them into integers? In java, there's functions to determine whether the string is a letter or a number.. but how about c++??


Report Offensive Message For Removal


Response Number 1
Name: BlueRaja
Date: September 26, 2004 at 20:16:16 Pacific
Reply: (edit)

int chartoint(char c)
{
if((c>='0')&&(c<='9'))
return(c-'0');
else return 0;
}

AKhalifman@hotmail.com


Report Offensive Follow Up For Removal

Response Number 2
Name: Savage2k5
Date: September 26, 2004 at 20:26:07 Pacific
Reply: (edit)

You can compare the the read character to other characters out there. '0' is equal to 48 in ascii, and '9' is equal to 57 in ascii. So any chars casted to ints that fall between 48 -> 57 is a number. Then all you have to do is subtract 48 from the ascii value to get the real number value.

char temp = array[i];
int result;
if ((int)temp > 47 && (int)temp < 58)
result = (int)temp - 48; //offset of ascii


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: re: converting char array into int

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge