Convert Cstring to Const Char*

Convert CString to const char*

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.

How to convert CString to const char*?

This line

const char* cstr = (LPCTSTR)CString;

doesn't compile because I guess you are building an UNICODE build of your project and therefore _T expands to a 2-byte character (a wide character).

To make this conversion working you could use the ATL macros. Like:

USES_CONVERSION;
const char* cstr = T2A((LPCTSTR)CString);

However, this is not really related to your initial problem as you are using anyways _tstof() which handles the _T issues for you.

[Edited]:

The mistake is somewhere else. The format string of the TRACE is not using the wright placeholder for a float/double. Instead of %d use %f:

CString leftDigit = _T("12.5");
double firstNum = _tstof(leftDigit);
TRACE(_T("%f\n"), firstNum);

I tried this and got 12.50000000 printed in the Output pane of VS.

Converting form CString to const char*

The value of charstr gets destroyed at the end of the function before the caller assigns it to variable.

You don't need a function, the caller can use CStringA directly and note that test is valid before sFilePathA goes out of scope.

CStringA sFilePathA(filePath);
const char *test = sFilePathA;

Problem: How to convert CString into const char * in C++ MFC

CString casts to const char * directly

CString temp;
temp = "Wow";
const char * foo = (LPCSTR) temp;
printf("%s", foo);

will print 'foo'

Newer version of MFC also support the GetString() method:

CString temp;
temp = "Wow";
const char * foo = temp.GetString();
printf("%s", foo);

Convert wide CString to char*

an answer that will work in ALL cases, not a specific instance...

There is no such thing.

It's easy to convert "ABCD..." from wchar_t* to char*, but it doesn't work that way with non-Latin languages.

Stick to CString and wchar_t when your project is unicode.

If you need to upload data to webpage or something, then use CW2A and CA2W for utf-8 and utf-16 conversion.

CStringW unicode = L"Россия";
MessageBoxW(0,unicode,L"Russian",0);//should be okay

CStringA utf8 = CW2A(unicode, CP_UTF8);
::MessageBoxA(0,utf8,"format error",0);//WinApi doesn't get UTF-8

char buf[1024];
strcpy(buf, utf8);
::MessageBoxA(0,buf,"format error",0);//same problem

//send this buf to webpage or other utf-8 systems
//this should be compatible with notepad etc.
//text will appear correctly
ofstream f(L"c:\\stuff\\okay.txt");
f.write(buf, strlen(buf));

//convert utf8 back to utf16
unicode = CA2W(buf, CP_UTF8);
::MessageBoxW(0,unicode,L"okay",0);

CString to const char * _Source in function strcpy_s

CString has a LPCTSTR conversion operator. The compiler calls this to convert your CString to the required const char *. So what the compiler does is:

strcpy_s(buff, (const char*)params);

which is equivalent to

strcpy_s(buff, (LPCTSTR)params);

which is like

strcpy_s(buff, params.operator LPCTSTR());

This only works if you are not compiling for Unicode. On Unicode LPCTSTR is not const char * but const WCHAR *, so this would not work.



Related Topics



Leave a reply



Submit