Converting CString to int in Visaul C++, please help.
-
Hi, I have two variables with types CString and int. int nWidth; CString strWidth; strWidth = "110"; How do I convert strWidth("110") to int type(110) so I can store it in nWidth? Please show me how to do this, thank you very much. :)
use the atoi or atol functions. iWidth = atoi(strWidth);
-
Hi, I have two variables with types CString and int. int nWidth; CString strWidth; strWidth = "110"; How do I convert strWidth("110") to int type(110) so I can store it in nWidth? Please show me how to do this, thank you very much. :)
int nWidth = 110; CString strWidth; strWidth.Format ( "%d", nWidth ); // like the sprintf function
-Ben --------- On the topic of code with no error handling -- It's not poor coding, it's "optimistic" ;) -
Hi, I have two variables with types CString and int. int nWidth; CString strWidth; strWidth = "110"; How do I convert strWidth("110") to int type(110) so I can store it in nWidth? Please show me how to do this, thank you very much. :)
-
Use function StrToInt this way int nWidth=StrToInt(strWidth); just one line of code and u got inr value.;) Gonna be 18 till I die
Two problems. First, why use a non standard function when atoi works perfectly well, and second ( from MSDN) Windows NT/2000: Requires Windows 2000 (or Windows NT 4.0 with Internet Explorer 4.0 or later). Windows 95/98: Requires Windows 98 (or Windows 95 with Internet Explorer 4.0 or later). Sounds like a bad idea to me, unless you specifically do not target the platforms not supported, and even then I would personally prefer not to get into the habit of using something I may find unavailable to me down the track, for no good reason I can see. Christian #include "std_disclaimer.h" People who love sausage and respect the law should never watch either one being made. The things that come to those who wait are usually the things left by those who got there first.
-
use the atoi or atol functions. iWidth = atoi(strWidth);
Or you could use strtod() and strtol(). strtol() is useful if you want to read a number that is not in base 10. For example to read hex numbers use strtol("0x12345678", NULL, 16) _tcstol() and _tcstod() are the macros for UNICODE/MBCS portability Stephen Kellett -- C++/Java/Win NT/Unix variants Memory leaks/corruptions/performance/system problems. UK based. Problems with RSI/WRULD? Contact me for advice.