Answer the question
In order to leave comments, you need to log in
WideCharToMultiByte msvc 2012 bug?
Hello. There is a certain string
conversion function:wchar_t*
bool WcharToString(const wchar_t* wstr, std::string& converted, UINT codePage = CP_ACP)
{
if(wstr)
{
int length = ::WideCharToMultiByte(codePage, 0, wstr, -1, NULL, 0, NULL, NULL);
char* str = new char[length + 1];
str[length] = '\0';
// ignoring returned value
::WideCharToMultiByte(codePage, 0, wstr, -1, str, length, NULL, NULL);
converted.assign(str);
delete [] str;
return true;
}
return false;
}
WideCharToMultiByte
in WinNls.h 2010 studio and in Stringapiset.h 2012 - i.e. something changed :) ). Answer the question
In order to leave comments, you need to log in
::WideCharToMultiByte(codePage, 0, wstr, -1, str, length, NULL, NULL);
A small correction: the size of the target buffer should not be length , but length+1 (after all, that's how much memory is allocated), then you don't have to insert the trailing zero manually, the function will do it itself. And try to explicitly specify the encoding instead of CP_ACP.
The following trick is often used for conversion:
string str = _bstr_t(wstr).operator char*();
If you are using 2012 studio why not write:
std::string utilities::utf16ToUtf8(const std::wstring& utf16)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> convert;
return convert.to_bytes(utf16.c_str());
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question