#### Archived

This topic is now archived and is closed to further replies.

# splitting a string into integers

This topic is 5200 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

i want to split an array of 4 TCHARs into 4 integer variables, i tried the following which didn''t work properly: TCHAR CharArray[4]; int1 = atoi(&CharArray[0]); int2 = atoi(&CharArray[1]); int3 = atoi(&CharArray[2]); int4 = atoi(&CharArray[3]); but the whole character array was converted and put into the int1 value (and probably the others aswell but havent checked) What am i doing wrong? Scotty.

##### Share on other sites
the penny has just dropped about the mixed up conversion function, stupid me.

However, i'm still getting the problem of pulling out individual chars. i.e. int1 is still being set to the whole array string.

can anyone help?

[edited by - sdrichardson on April 22, 2004 7:46:38 PM]

##### Share on other sites
Are you saying you have a string of 4 digits of ''0'' through ''9'' and you want to extract those single digits? If so, then just do

int1 = CharArray[0] - ''0'';
int2 = CharArray[1] - ''0'';
int3 = CharArray[2] - ''0'';
int4 = CharArray[3] - ''0'';

##### Share on other sites
nope,

TCHAR CharArray[4] = {''1'',''2'',''3'',''4''};

i want each of these values in its own integer variable.

##### Share on other sites
oh, i think i see what you mean there, never thought of that, anyway to took a different approach and it was successful.

Scotty.

##### Share on other sites
aprosenf had the right way, but here''s a general function:

int *GetDigits(char *pcString){    int *pDigits;    int StrLen;    int i;    StringLen = strlen(pcString);    pDigits = (int *)malloc(StringLen);    for(i = 0; i < StringLen; i++)    {        pDigits[i] = pcString[i] - ''0'';    }    return pDigits;}

This returns an array of integers of digits from the string.

##### Share on other sites
..Except in your case, you''ll want to use _T(''0'') instad of ''0''. Since you''re using TCHARs.

I like pie.

1. 1
Rutin
23
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 30
• 41
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631741
• Total Posts
3001980
×