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
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++??
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
Summary: If you want to convert a char array into an int, you could use the atoi() function. { int x; char number[] = "12345"; x = atoi(number); } Then x is an integer equal to 12345. ...
Summary: here's my C coding problem: how does one convert a char array to an int array? for example, say if one has the pattern = "3 3 3 3 3 4" stored as chars in an array (with spaces) how can i convert this...
Summary: I'm trying to build a concole program that will disassemble files into fairly equal divisions to be reassembled later (for floppy disk transfer of a file >1.44mb, for example). It creates the division...