我已经阅读了有关WideCharToMultiByte的文档,但我坚持这个参数:
lpMultiByteStr [out] Pointer to a buffer that receives the converted string.
我不太确定如何正确初始化变量并将其输入函数
这里有几个函数(基于Brian Bondy的例子),使用WideCharToMultiByte和MultiByteToWideChar在std :: wstring和std :: string之间转换,使用utf8不丢失任何数据.
// Convert a wide Unicode string to an UTF8 string std::string utf8_encode(const std::wstring &wstr) { if( wstr.empty() ) return std::string(); int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL); std::string strTo( size_needed, 0 ); WideCharToMultiByte (CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL); return strTo; } // Convert an UTF8 string to a wide Unicode String std::wstring utf8_decode(const std::string &str) { if( str.empty() ) return std::wstring(); int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0); std::wstring wstrTo( size_needed, 0 ); MultiByteToWideChar (CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed); return wstrTo; }
详细阐述Brian R. Bondy提供的答案:这是一个示例,说明为什么不能简单地将输出缓冲区的大小调整为源字符串中的宽字符数:
#include#include #include #include /* string consisting of several Asian characters */ wchar_t wcsString[] = L"\u9580\u961c\u9640\u963f\u963b\u9644"; int main() { size_t wcsChars = wcslen( wcsString); size_t sizeRequired = WideCharToMultiByte( 950, 0, wcsString, -1, NULL, 0, NULL, NULL); printf( "Wide chars in wcsString: %u\n", wcsChars); printf( "Bytes required for CP950 encoding (excluding NUL terminator): %u\n", sizeRequired-1); sizeRequired = WideCharToMultiByte( CP_UTF8, 0, wcsString, -1, NULL, 0, NULL, NULL); printf( "Bytes required for UTF8 encoding (excluding NUL terminator): %u\n", sizeRequired-1); }
并输出:
Wide chars in wcsString: 6 Bytes required for CP950 encoding (excluding NUL terminator): 12 Bytes required for UTF8 encoding (excluding NUL terminator): 18
您可以通过创建新的char数组来使用lpMultiByteStr [out]参数.然后传入此char数组以使其填充.您只需要初始化字符串+ 1的长度,以便在转换后可以使用空终止字符串.
以下是一些有用的辅助函数,它们显示了所有参数的用法.
#includestd::string wstrtostr(const std::wstring &wstr) { // Convert a Unicode string to an ASCII string std::string strTo; char *szTo = new char[wstr.length() + 1]; szTo[wstr.size()] = '\0'; WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, szTo, (int)wstr.length(), NULL, NULL); strTo = szTo; delete[] szTo; return strTo; } std::wstring strtowstr(const std::string &str) { // Convert an ASCII string to a Unicode String std::wstring wstrTo; wchar_t *wszTo = new wchar_t[str.length() + 1]; wszTo[str.size()] = L'\0'; MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, wszTo, (int)str.length()); wstrTo = wszTo; delete[] wszTo; return wstrTo; }
-
在文档中的任何时候当你看到它有一个参数是一个指向一个类型的指针,并且它们告诉你它是一个out变量时,你会想要创建那个类型,然后传入指向它的指针.该函数将使用该指针填充您的变量.
所以你可以更好地理解这一点:
//pX is an out parameter, it fills your variable with 10. void fillXWith10(int *pX) { *pX = 10; } int main(int argc, char ** argv) { int X; fillXWith10(&X); return 0; }