splitting a string into integers
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.
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]
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]
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'';
int1 = CharArray[0] - ''0'';
int2 = CharArray[1] - ''0'';
int3 = CharArray[2] - ''0'';
int4 = CharArray[3] - ''0'';
nope,
TCHAR CharArray[4] = {''1'',''2'',''3'',''4''};
i want each of these values in its own integer variable.
TCHAR CharArray[4] = {''1'',''2'',''3'',''4''};
i want each of these values in its own integer variable.
oh, i think i see what you mean there, never thought of that, anyway to took a different approach and it was successful.
Scotty.
Scotty.
aprosenf had the right way, but here''s a general function:
This returns an array of integers of digits from the string.
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.
..Except in your case, you''ll want to use _T(''0'') instad of ''0''. Since you''re using TCHARs.
I like pie.
I like pie.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement