Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cleaner way to convert from CStringW to std::string?

Tags:

c++

string

I figured out a somewhat convoluted way to convert a CStringW to a std::string, but I was wondering if there's a cleaner way than:

CStringW cwstr;
std::wstring stdwstr = cwstr;
std::string stdstr = CW2T(stdwstr.c_str());

2 Answers

You can cut out the intermediate std::wstring:

CStringW cwstr;
std::string stdstr = CW2A(cwstr);

Also note that you want the CW2A macro for correctness. CW2T converts to a TCHAR string, so the code you posted would only compile for an ANSI build (where TCHAR is char).

like image 121
Nick Meyer Avatar answered May 04 '26 05:05

Nick Meyer


std::string str = CStringA( cwstr );

Job done.

like image 39
Goz Avatar answered May 04 '26 04:05

Goz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!