# Reading numbers form a CString and adding them

I have a CString, say:

CString testString = "abc123";

I want to add the 123, add convert the new number to a string.

I don't need to search the string as I know the positons of the digits I want to add (in this case positions 3,4, and 5..... 0 based)

thanks for any help!
Asked:
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
You could use

int n = 123;
CString testString = "abc";
CString numString = testString.Right(testString.Length() - 3); // get a string onlys containing the numbers
testString = testString.Left(3); // cut off the old numbers

int tmp = atoi((LPCTSTR)numString);

numString.Format("%d", n + tmp);

testString = testString + numString; // put the parts together again
0
Author Commented:
Thanks, but I don't think I was very clear on what I need to do, I'll try to clarify:

I have a CString "@@@@12@@@"  where 1 and 2 are the two numbers I want to add and @ may be #'s or letters.

I want to do 1 + 2 = 3

then convert the 3 to a CString.

the numbers will always be single digit (0-9)

thanks again.

0
Commented:
Aaah, OK, then it is

CString testString = "abc123";
CString strNum;
int n1 = testString[3] - '0'; // subtracting ASCII '0' will give the numeric value
int n2 = testString[4] - '0';

strNum.Format("%d", n1 + n2);

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
thanks!
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.