Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you convert CString and std::string std::wstring to each other?

CString is quite handy, while std::string is more compatible with STL container. I am using hash_map. However, hash_map does not support CStrings as keys, so I want to convert the CString into a std::string.

Writing a CString hash function seems to take a lot of time.

CString -----> std::string 

How can I do this?

std::string -----> CString:  inline CString toCString(std::string const& str) {     return CString(str.c_str());  } 

Am I right?


EDIT:

Here are more questions:

How can I convert from wstring to CString and vice versa?

// wstring -> CString std::wstring src; CString result(src.c_str());  // CString -> wstring CString src; std::wstring des(src.GetString()); 

Is there any problem with this?

Additionally, how can I convert from std::wstring to std::string and vice versa?

like image 629
user25749 Avatar asked Nov 03 '08 06:11

user25749


People also ask

How do you convert std::wstring to CString?

The easiest solution is to use Unicode string literals and std::wstring: wstring z = L"nüşabə"; CString cs(z. c_str()); nameData. SetWindowTextW(cs);

How do you convert CString to Lpcwstr?

For example, the macro to convert CString to LPCWSTR is CT2W(s) . Another way is to use the specialized CStringA and CStringW classes. These are the corresponding ascii and wide versions of CString depending on if you're compile with the UNICODE flag.

Does CString include string?

CString accepts NULL-terminated C-style strings. CString tracks the string length for faster performance, but it also retains the NULL character in the stored character data to support conversion to LPCWSTR . CString includes the null terminator when it exports a C-style string.

How do I copy a CString to a char array?

This can be done with the help of c_str() and strcpy() function of library cstring. The c_str() function is used to return a pointer to an array that contains a null terminated sequence of character representing the current value of the string.


1 Answers

According to CodeGuru:

CString to std::string:

CString cs("Hello"); std::string s((LPCTSTR)cs); 

BUT: std::string cannot always construct from a LPCTSTR. i.e. the code will fail for UNICODE builds.

As std::string can construct only from LPSTR / LPCSTR, a programmer who uses VC++ 7.x or better can utilize conversion classes such as CT2CA as an intermediary.

CString cs ("Hello"); // Convert a TCHAR string to a LPCSTR CT2CA pszConvertedAnsiString (cs); // construct a std::string using the LPCSTR input std::string strStd (pszConvertedAnsiString); 

std::string to CString: (From Visual Studio's CString FAQs...)

std::string s("Hello"); CString cs(s.c_str()); 

CStringT can construct from both character or wide-character strings. i.e. It can convert from char* (i.e. LPSTR) or from wchar_t* (LPWSTR).

In other words, char-specialization (of CStringT) i.e. CStringA, wchar_t-specilization CStringW, and TCHAR-specialization CString can be constructed from either char or wide-character, null terminated (null-termination is very important here) string sources.
Althoug IInspectable amends the "null-termination" part in the comments:

NUL-termination is not required.
CStringT has conversion constructors that take an explicit length argument. This also means that you can construct CStringT objects from std::string objects with embedded NUL characters.

like image 169
VonC Avatar answered Sep 20 '22 03:09

VonC