Archived

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

splitting a string into integers

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites