CString to iny

Can i convert an CString to int. Example i have string str "123" and how should i convert him to int 123 ?????
p_vladimirAsked:
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.

DexstarCommented:
p_vladimir:

> Can i convert an CString to int. Example i have string str "123" and how should
> i convert him to int 123 ?????

CString str = "123";
int i = atoi(str);

Hope That Helps,
Dex*
0
Karl Heinz KremerCommented:
atoi() will work as long as you are not using Unicode (or MBCS) strings. In this case, you need to use _wtoi()
0
DexstarCommented:
Actually, use _ttoi and then the compiler will work out the UNICODE / Ansi string issues for you.

CString str = "123";
int i = _ttoi(str);
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.

Start your 7-day free trial
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Drew BentonCommented:
Even though the points have been awarded, heres how you do it using atoi().

int i = atoi(str.operator LPCTSTR());

*This works b/c LPCSTR is a "char*"
0
Drew BentonCommented:
Ops i appologize to Dexstar, read the page yet missed his comment, sry!
0
Drew BentonCommented:
Now i gotta appologize for the generic use of "his"...*sigh* late night programming and forum responding mix as well as derefrencing the NULL pointer ;)
0
DexstarCommented:
No apology necessary...  I'm a guy.  :)

D*
0
Drew BentonCommented:
k cool :)
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.