17

I have a std::string. I need to convert this std:string to a Cstring.

I try to use the .c_str() but it's only for non-unicode project and i use unicode project ( because non unicode project are depreceated wiht VS2013).

Anyone could show my how to convert an std::string to a CString in unicode project ?

4 Answers 4

45

CString has a conversion constructor taking a const char* (CStringT::CStringT). Converting a std::string to a CString is as simple as:

std::string stdstr("foo");
CString cstr(stdstr.c_str());

This works for both UNICODE and MBCS projects. If your std::string contains embedded NUL characters you have to use a conversion constructor with a length argument:

std::string stdstr("foo");
stdstr += '\0';
stdstr += "bar";
CString cstr(stdstr.c_str(), stdstr.length());

Note that the conversion constructors implicitly use the ANSI code page of the current thread (CP_THREAD_ACP) to convert between ANSI and UTF-16 encoding. If you cannot (or do not want to) change the thread's ANSI code page, but still need to specify an explicit code page to use for the conversion, you have to use another solution (e.g. the ATL and MFC String Conversion Macros).

18
  • I get cstr identifier is undefined. Where is it defined?
    – Kala J
    Jan 20, 2016 at 19:56
  • 2
    @MihaiTodor: Since C++11 there is no difference. From std::basic_string::data: "The returned array is null-terminated, that is, data() and c_str() perform the same function." May 5, 2016 at 21:51
  • 1
    Answering a question on converting from std::string to CString in Unicode projects without mentioning code pages and encodings is incomplete at best. What is the encoding used in the std::string? Unicode UTF-8? Some other encoding/code page? The OP should use MultiByteToWideChar for the conversion, specifying the proper encoding. Moreover, MSDN doc for CString advises against using embedded NULs with it.
    – Mr.C64
    Jan 27, 2017 at 15:01
  • 1
    @ser That's covered in the final sentence of this answer. May 7, 2021 at 19:21
  • 1
    @ser If you have a narrow character string you have to know, what character encoding it uses. There's nothing in the character encoding or CStringA's implementation that would help you. It's an inherent problem with narrow character strings, unrelated to the container you use to store it. With (conforming) XML things are easier: Only UTF-8 and UTF-16 are allowed, unless the encoding is explicitly specified. XML is predominantly used with UTF-8, so your best bet would be to use CP_UTF8 as the code page for the character conversion. May 9, 2021 at 4:58
3

Unicode CString's constructor accepts char* so you can do this:

std::string str = "string";
CString ss(str.c_str());
2

Use the ATL conversion macros. They work in every case, when you use CString. CString is either MBCS or Unicode... depends on your Compiler Settingss.

std::string str = "string";
CString ss(CA2T(str.c_str());
3
  • 2
    You're trying too hard. There is no need for a conversion macro with all the conversion constructors of CString. This also fails for std::string objects with embedded NUL characters. Oct 27, 2013 at 22:34
  • CString is not always used in conjunction with ATL. It is a class shared between MFC and ATL. Jan 8, 2018 at 20:39
  • @MichaelHaephrati: It's not clear, what point you are trying to make. Are you objecting against the term "ATL conversion macros"? That's slightly inaccurate. They are called ATL and MFC String Conversion Macros, and have been going by that name for years, probably decades even. Jul 23, 2018 at 21:07
-2

Bonus: if you use the conversion frequently you can define a macro:

#define STDTOCSTRING(s) CString(s.c_str())

so your code is more readable.

5
  • 6
    Using a preprocessor macro when a function can be used instead is a capital crime. May 5, 2016 at 21:53
  • Of course. Let's hang every programmer that ever used a macro to avoid a function call in a frequent low-level operation.
    – iKanor
    May 7, 2016 at 12:22
  • You need to inspect the code your compiler generates more often. This function is very likely inlined. And even if it isn't, there's still RVO so the overhead is negligible. As always, don't optimize unless there is a reason to. May 7, 2016 at 17:04
  • I'm very well aware of inline optimisations. My point was that there are absolute rules in development. Thus, considering something a capital crime adds unnecessary drama and misleads your readers. Your second comment is much more insightful than the first one, by the way. Thanks for explaining further your point of view.
    – iKanor
    May 8, 2016 at 11:56
  • 1
    There cannot ever be enough drama when someone proposes to use a macro that has nothing at all going for it. Use a function. It will be safer, and have the same performance characteristics (the real cost is allocating dynamic storage from the heap - everything else, including a potential function call, fades into statistical noise). May 8, 2016 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.