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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

re: converting char array into int

Reply to Message Icon

Name: mydreams84
Date: September 26, 2004 at 08:02:38 Pacific
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++??



Sponsored Link
Ads by Google

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

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

AKhalifman@hotmail.com


0

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

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: re: converting char array into int

how to convert chars to int www.computing.net/answers/programming/how-to-convert-chars-to-int/11828.html

c: convert char a[] to int b[] www.computing.net/answers/programming/c-convert-char-a-to-int-b/4572.html

Resetting a char array www.computing.net/answers/programming/resetting-a-char-array/13514.html