C++ 如何将 TCHAR 数组转换为 std::string?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6291458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:51:42  来源:igfitidea点击:

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

c++stringwindowsunicodetchar

提问by ashmish2

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

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

回答by Alok Save

TCHARis just a typedef that, depending on your compilation configuration, either defaults to charor wchar_t.

TCHAR只是一个 typedef,根据您的编译配置,默认为charwchar_t.

Standard Template Library supports both ASCII (with std::string) and wide character sets (with std::wstring). All you need to do is to typedefString 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)。所有你需要做的是对的typedefString作为任何的std :: string或std ::根据您的编译配置wstring的。为了保持灵活性,您可以使用以下代码:

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

Now you may use Stringin your code and let the compiler handle the nasty parts. String will now have constructors that lets you convert TCHARto std::stringor std::wstring.

现在您可以String在您的代码中使用并让编译器处理讨厌的部分。String 现在将具有构造函数,可让您转换TCHARstd::stringstd::wstring

回答by Naszta

TCHAR type is charor wchar_t, depending on your project settings.

TCHAR 类型为charwchar_t,具体取决于您的项目设置。

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

So if you must use std::stringinstead of std::wstring, you should use a converter function. I may use wcstombsor WideCharToMultiByte.

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

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

回答by Bo Persson

TCHAR is either char or wchar_t, so a

TCHAR 要么是 char 要么是 wchar_t,所以

typedef basic_string<TCHAR>   tstring;

is one way of doing it.

是一种方法。

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

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

回答by Aemmel

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!

回答by serup

Quick and dirty solution :

快速而肮脏的解决方案:

TCHAR str[256] = {};

// put something in str...


// convert to string
std::string strtmp(&str[0], &str[255]);

std::cout << strtmp << std::endl;

回答by user1567759

Simple!

简单的!

char* tcharToChar(TCHAR* buffer)
{
    char *charBuffer = NULL;
    int lengthOfbuffer = lstrlenW(buffer);
    if(buffer!=NULL)
    {
        charBuffer = (char*)calloc(lengthOfbuffer+1,sizeof(char));
    }
    else
    {
        return NULL;
    }

    for (int index = 0;
        index < lengthOfbuffer;
        index++)
    {
        char *singleCharacter = (char*)calloc(2,sizeof(char));
        singleCharacter[0] = (char)buffer[index];
        singleCharacter[1] = '##代码##';
        strcat(charBuffer, singleCharacter);
    }
    strcat(charBuffer, "##代码##");
    return charBuffer;

}

Then capture the char * in std::string

然后在 std::string 中捕获 char *