c ++ char数组未正确转换为std :: string

时间:2016-07-06 17:29:00

标签: c++ arrays string winapi

在win32上下文中开发并尝试从我的下拉列表中获取输入作为字符串。 strTextchar[255]类型中的正确值。我试图将这个char数组转换为字符串,但它失败了

char strText[13];
SendMessage(dropDown, CB_GETLBTEXT, dropDownSelection, reinterpret_cast<LPARAM>(strText));

std::string test(strText);  // outputs W
std::string test2("WORKS"); // outputs WORKS

感谢任何提示

**编辑**

strText

strText 0x003beeec "M"  char[13]
77 'M'
0 '\0'
79 'O'
0 '\0'
78 'N'
0 '\0'
71 'G'
0 '\0'
79 'O'
0 '\0'
68 'D'
0 '\0'
66 'B'

1 个答案:

答案 0 :(得分:4)

最有可能的是,您的程序是以Unicode模式编译的,因此strText会收到UTF-16编码的字符串。由于W字符适合单个字节,因此第二个字节将为0(小端编码)。

在Unicode模式下与Windows API连接时,请勿使用char。请改用wchar_tstd::wstring

或者阅读并理解this question and its answers并自己决定如何处理字符串。

Read more about Unicode in the Windows APIAnd about Unicode in general

相关问题