I was wondering if there's a way to convert a CString or a char *x[10] to a int? I know I can convert int to CString, but can it go the other way?
Thanx,
Atrus

Recommended Answers

All 7 Replies

Sure, why not?

atoi for converting a char * to an int C or Or stringstreams for C++

for CString (I assume you mean Microsoft MFC CString class)

// convert from CString to int just requires typcasting the CString
CString str = "123";
int n = atoi((LPCTSTR)str);

// convert from int to CString
str.Format("%d", n);

ok so would the following statement work then?

char *str[5];
str[0] = "123";
int n = atoi((LPCTSTR)str[0]);

and yes I meant MFC CStrings... Sorry I didn't mentionthat

When in doubt, test it out!

yeah I thought of that AFTER I posted :P

ok so would the following statement work then?

char *str[5];
str[0] = "123";
int n = atoi((LPCTSTR)str[0]);

you don't need to typecast character arrays -- that typecast is to convert a CString object to const char*.

char *str[5];
str[0] = "123";
int n = atoi(str[0]);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.