[翻译]  How to convert a TCHAR array to std::string?

[CHINESE]  如何将TCHAR数组转换为std :: string?


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

如何将TCHAR数组转换为std :: string(而不是std :: basic_string)?

4 个解决方案

#1


32  

TCHAR is just a typedef that, depending on your compilation configuration, either defaults to char or wchar.

TCHAR只是一个typedef,根据您的编译配置,默认为char或wchar。

Standard Template Library supports both ASCII (with std::string) and wide character sets (with std::wstring). All you need to do is to typedef String as either std::string or std::wstring depending on your compilation configuration. To maintain flexibility you can use the following code:

标准模板库支持ASCII(带有std :: string)和宽字符集(带有std :: wstring)。您需要做的就是将typedef String作为std :: string或std :: wstring,具体取决于您的编译配置。为了保持灵活性,您可以使用以下代码:

#ifndef UNICODE  
  typedef std::string String; 
#else
  typedef std::wstring String; 
#endif

Now you may use String in your code and let the compiler handle the nasty parts. String will now have constructors that lets you convert TCHAR to std::string or std::wstring.

现在,您可以在代码中使用String,让编译器处理令人讨厌的部分。 String现在将具有允许您将TCHAR转换为std :: string或std :: wstring的构造函数。

#2


5  

TCHAR type is char or wchar_t, depending on your project settings.

TCHAR类型是char或wchar_t,具体取决于您的项目设置。

 #ifdef UNICODE
     // TCHAR type is wchar_t
 #else
     // TCHAR type is char
 #endif

So if you must use std::string instead of std::wstring, you should use a converter function. I may use wcstombs or WideCharToMultiByte.

因此,如果必须使用std :: string而不是std :: wstring,则应使用转换器函数。我可以使用wcstombs或WideCharToMultiByte。

TCHAR * text;

#ifdef UNICODE
    /*/
    // Simple C
    const size_t size = ( wcslen(text) + 1 ) * sizeof(wchar_t);
    wcstombs(&buffer[0], text, size);
    std::vector<char> buffer(size);
    /*/
    // Windows API (I would use this)
    std::vector<char> buffer;
    int size = WideCharToMultiByte(CP_UTF8, 0, text, -1, NULL, 0, NULL, NULL);
    if (size > 0) {
        buffer.resize(size);
        WideCharToMultiByte(CP_UTF8, 0, text, -1, static_cast<BYTE*>(&buffer[0]), buffer.size(), NULL, NULL);
    }
    else {
        // Error handling
    }
    //*/
    std::string string(&buffer[0]);
#else
    std::string string(text);
#endif

#3


4  

TCHAR is either char or wchar_t, so a

TCHAR是char或wchar_t,所以a

typedef basic_string<TCHAR>   tstring;

is one way of doing it.

这是一种做法。

The other is to skip char altogether and just use std::wstring.

另一种是完全跳过char并使用std :: wstring。

#4


2  

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):

我的答案很晚,我会承认,但是通过'Alok Save'和一些研究的答案,我找到了一个好方法! (注意:我没有对这个版本进行过多次测试,所以它可能不适用于所有情况,但是根据我测试它应该):

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!

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2018 ITdaan.com 粤ICP备14056181号