CString to char*

Flaviu_ 1,031 Reputation points
2020-11-01T08:44:43.607+00:00

I have a SDI unicode project. I was tried:

CString s(_T("ala bala portocala"));
char* c = (char*)(LPCTSTR)s;

but in c I have 'a' only. When I tried:

CString s(_T("ala bala portocala"));
char* c = s.GetBuffer(s.GetLength());
s.ReleaseBuffer();

I got:

error C2440: 'initializing' : cannot convert from 'wchar_t *' to 'char *' 

How to do the right conversion ?

Developer technologies | C++
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2020-11-01T09:18:06.687+00:00

    If you do not want to modify the string, then try one of solutions:

    CString s( _T( "ala bala portocala" ) );
    CStringA s2( s );
    const char* c = s2;
    

    C becomes invalid when s2 is destroyed.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.