[英]c++ char array not correctly converted to std::string
Developing in win32 context and trying to get the input from my dropdown as a string. 在Win32背景下发展,并试图从我的下拉作为一个字符串获得输入。
strText
is the correct value in type char[255]
. strText
是char[255]
类型中的正确值。 I'm trying to convert this char array to a string, but it fails 我正在尝试将此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
thanks for any hints 谢谢你的提示
** EDIT ** **编辑**
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'
Most likely, your program is compiled in Unicode mode, so strText
receives an UTF-16 encoded string. 很可能您的程序是在Unicode模式下编译的,因此
strText
会接收UTF-16编码的字符串。 Since the W
character fits in a single byte, the second byte will be 0 (little-endian encoding). 由于
W
字符适合单个字节,因此第二个字节将为0(小尾数编码)。
Don't use char
when interfacing with the Windows API in Unicode mode. 在Unicode模式下与Windows API交互时,请勿使用
char
。 Use wchar_t
and and std::wstring
instead. 使用
wchar_t
和std::wstring
代替。
Or read and understand this question and its answers and make up your own mind on how you want to handle strings. 或阅读并理解此问题及其答案,并决定如何处理字符串。
Read more about Unicode in the Windows API . 在Windows API中阅读有关Unicode的更多信息 。 And about Unicode in general .
关于Unicode 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.