How do I convert from CString
to const char*
in my Unicode MFC application?

- 24,552
- 19
- 101
- 135

- 17,632
- 38
- 139
- 202
-
5The fact that you are building a unicode program is KEY to this problem - PLEASE add this info to the question. (or someone with rep do it?) – Aardvark May 13 '09 at 18:16
9 Answers
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.

- 76,700
- 56
- 158
- 197
-
i already got the answer to the question a little bit earlier and you're right, I used the CT2A macro. Thanks. – Attilah May 14 '09 at 18:06
-
-
1`CA2T` does **not** convert to Unicode. It converts to a `TCHAR` which may or may not represent Unicode characters. `CA2[C]W` is the macro that converts to Unicode. – IInspectable Dec 22 '12 at 21:45
-
The `CT2A ascii(str, CP_UTF8);` line solved the problem I reported on comments of http://stackoverflow.com/a/5551013/383779. Thanks. – sergiol Dec 06 '16 at 16:31
-
2Btw, none of the objects named `ascii` are in fact ASCII encoded strings. They are *narrow character strings*, using ANSI, UTF-8, and ANSI encoding (in that order). – IInspectable Feb 21 '17 at 10:05
If your CString is Unicode, you'll need to do a conversion to multi-byte characters. Fortunately there is a version of CString which will do this automatically.
CString unicodestr = _T("Testing");
CStringA charstr(unicodestr);
DoMyStuff((const char *) charstr);

- 299,747
- 42
- 398
- 622
-
1when i use this, i get this error : "Error 2 error C2440: 'initializing' : cannot convert from 'CString' to 'ATL::CStringT
'" " – Attilah May 13 '09 at 17:53 -
1@Attilah: Thanks for catching that, I had the syntax wrong. Fixed. – Mark Ransom May 13 '09 at 18:03
Note: This answer predates the Unicode requirement; see the comments.
Just cast it:
CString s;
const TCHAR* x = (LPCTSTR) s;
It works because CString has a cast operator to do exactly this.
Using TCHAR makes your code Unicode-independent; if you're not concerned about Unicode you can simply use char
instead of TCHAR
.

- 272,464
- 47
- 358
- 399
-
1when i try your method, i get this error : "Error 1 error C2664: 'CppSQLite3DB::execDML' : cannot convert parameter 1 from 'const TCHAR *' to 'const char *'". my projetc settings use Unicode but the function CppSQLite3DB::execDML requires a const char* parameter. – Attilah May 13 '09 at 17:57
-
1As Mark says, you need to convert from a Unicode CString to an ANSI CStringA: CStringA charstr(unicodestr); You can then cast the CStringA to a const char* – RichieHindle May 13 '09 at 18:58
There is an explicit cast on CString to LPCTSTR, so you can do (provided unicode is not specified):
CString str;
// ....
const char* cstr = (LPCTSTR)str;

- 554,122
- 78
- 1,158
- 1,373
-
I got this error message : error C2440: 'type cast' : cannot convert from 'CString' to 'LPCSTR' – someone_ smiley Mar 25 '13 at 03:39
-
2This answer is correct for ANSI CString. In case of Unicode CString, see the accepted answer. – amolbk Feb 03 '15 at 09:04
I recommendo to you use TtoC from ConvUnicode.h
const CString word= "hello";
const char* myFile = TtoC(path.GetString());
It is a macro to do conversions per Unicode

- 11
- 5
Generic Conversion Macros (TN059 Other Considerations section is important):
A2CW (LPCSTR) -> (LPCWSTR)
A2W (LPCSTR) -> (LPWSTR)
W2CA (LPCWSTR) -> (LPCSTR)
W2A (LPCWSTR) -> (LPSTR)

- 2,546
- 2
- 22
- 30
I used this conversion:
CString cs = "TEST";
char* c = cs.GetBuffer(m_ncs me.GetLength())
I hope this is useful.

- 77
- 1
- 9
-
In it's current form this code shouldn't work - what's "me"? what's "m_ncs"? – ashrasmun Dec 29 '22 at 11:38
I got it from Microsoft site and fullfill my problem, so sharing here CString s(_T("ala bala portocala")); char* c = (char*)(LPCTSTR)s;

- 1
- 1
-
Note that the OP is using **Unicode** - so the `CString` will be composed of `wchar_t` elements and a simple cast like you have shown will almost never work. – Adrian Mole May 11 '23 at 14:53
-
This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34371682) – user16217248 May 15 '23 at 23:28