How do I convert from CString
to const char*
in my Unicode MFC application?
To convert a TCHAR
CString to ASCII, use the CT2A
macro - this will also allow you to convert the string to UTF8 (or any other Windows code page):
// Convert using the local code page
CString str(_T("Hello, world!"));
CT2A ascii(str);
TRACE(_T("ASCII: %S\n"), ascii.m_psz);
// Convert to UTF8
CString str(_T("Some Unicode goodness"));
CT2A ascii(str, CP_UTF8);
TRACE(_T("UTF8: %S\n"), ascii.m_psz);
// Convert to Thai code page
CString str(_T("Some Thai text"));
CT2A ascii(str, 874);
TRACE(_T("Thai: %S\n"), ascii.m_psz);
There is also a macro to convert from ASCII -> Unicode (CA2T
) and you can use these in ATL/WTL apps as long as you have VS2003 or greater.
See the MSDN for more info.