Solved

Posted on 2004-11-10
166 Views
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!
0
Question by:galneweinhaw

LVL 86

Expert Comment

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 Comment

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

LVL 86

Accepted Solution

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

Author Comment

thanks!
0

## Featured Post

### Suggested Solutions

how do i create updater to My Activex application? 3 71
format the code in java 6 60
MaxSpan challenge 9 50
wordappend challenge 8 45
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…