CString to char*

Flaviu_ 971 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 ?

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,711 questions
{count} votes

Accepted answer
  1. Viorel 117K 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.