Category Archives: C++

C++ – Convert CString into int

You can use _wtoi() or _ttoi() to convert a CString into int.

#include "atlstr.h"
#include <iostream>
using namespace std;

int main ()
{
	// Create a CString with number
	CString intStr = _T("123");

	// Convert the CString into int
	int aInt = _wtoi(intStr);

	// 123 + 123 = 246
	aInt += 123;

	// Print 246 to cout
	cout << aInt << endl;

	// Pause
	system("PAUSE");

	// Terminate the program:
	return 0;
}

Continue reading C++ – Convert CString into int

C++ – Print CString to cout

The following code shows you how to print CString to cout.

#include &quot;atlstr.h&quot;
#include &lt;iostream&gt;
using namespace std;

int main ()
{
	// Create a CString
	CString cStr = _T(&quot;I am a CString.&quot;);
	
	// Print the address of cStr only
	cout &lt;&lt; cStr &lt;&lt; endl;

	// Print the cStr content
	wcout &lt;&lt; cStr.GetString() &lt;&lt; endl;

	// Pause
	system(&quot;PAUSE&quot;);

	// Terminate the program:
	return 0;
}

Continue reading C++ – Print CString to cout