How to convert a TCHAR array to std::string?

后端 未结 6 1523
孤街浪徒
孤街浪徒 2020-12-06 09:14

How do I convert a TCHAR array to std::string (not to std::basic_string)?

6条回答
  •  旧巷少年郎
    2020-12-06 09:48

    My answer is late, I'll admit that, but with the answers of 'Alok Save' and some research I've found a good way! (Note: I didn't test this version a lot, so it might not work in every case, but from what I tested it should):

    TCHAR t = SomeFunctionReturningTCHAR();
    std::string str;
    
    #ifndef UNICODE
        str = t;
    #else
        std::wstring wStr = t;
        str = std::string(wStr.begin(), wStr.end());
    #endif
    
    std::cout << str << std::endl; //<-- should work!
    

提交回复
热议问题