You are here:

C++/cstring to string



Hello, Bill

My question is simple
how can I convert from a cstring to a string?

I know the opposite conversion, but unfortunately cstring doesnt provide a function for converting to a string


Hi John,

There are CString operators for this: LPCSTR and LPCTSTR for char * and TCHAR * conversions.  TCHAR follows the UNICODE/ANSI compiler setting (char * wchar_t *) and is preferred for portability for this compiler setting.  Therefore, I'd use LPCTSTR.  If you want to force ANSI, use LPCSTR and nesting them may be neccessry: LPCSTR( LPCTSTR( str ) ).  


All Answers

Answers by Expert:

Ask Experts


Bill A


I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.


I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

BS Computer Engineering

©2017 All rights reserved.

[an error occurred while processing this directive]