61

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

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Attilah
  • 17,632
  • 38
  • 139
  • 202
  • 5
    The 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 Answers9

84

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.

Rob
  • 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
  • You could also do TRACE(_T("ASCII: %S\n"), CT2A(str).m_psz); – ajs410 Oct 07 '11 at 20:59
  • 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
  • 2
    Btw, 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
27

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);
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
18

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.

RichieHindle
  • 272,464
  • 47
  • 358
  • 399
  • 1
    when 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
  • 1
    As 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
6

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;
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
2

I had a similar problem. I had a char* buffer with the .so name in it.
I could not convert the char* variable to LPCTSTR. Here's how I got around it...

char *fNam;
...
LPCSTR nam = fNam;
dll = LoadLibraryA(nam);
tanascius
  • 53,078
  • 22
  • 114
  • 136
Mr.What
  • 21
  • 1
0

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

0

Generic Conversion Macros (TN059 Other Considerations section is important):

A2CW     (LPCSTR)  -> (LPCWSTR)  
A2W      (LPCSTR)  -> (LPWSTR)  
W2CA     (LPCWSTR) -> (LPCSTR)  
W2A      (LPCWSTR) -> (LPSTR) 
Amit G.
  • 2,546
  • 2
  • 22
  • 30
0

I used this conversion:

CString cs = "TEST";
char* c = cs.GetBuffer(m_ncs me.GetLength())

I hope this is useful.

afs_mp
  • 77
  • 1
  • 9
0

I got it from Microsoft site and fullfill my problem, so sharing here CString s(_T("ala bala portocala")); char* c = (char*)(LPCTSTR)s;

  • 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