// Convert a wide Unicode string to an UTF8 string
std::string UTF16ToUTF8(const std::wstring & wstr)
{
if ( wstr.empty() )
return "";
// with first call we get the required size. note, a utf8 character may have up to 4 bytes
int neededSize = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
// create a string buffer
std::string strUTF8(neededSize, '\0' );
WideCharToMultiByte (CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strUTF8[0], neededSite, NULL, NULL);
return strUTF8;
}
wchar_t * str1 = new wchar_t[255];
// better: wchar_t str1[255] = { 0 };
sprintf(str1,"%ls",str.c_str());
// better: wcscpy_s(str1, 255, str.c_str());
send_as_c_buffer((const char *)str.c_str(), str.length()*sizeof(wchar_t));
you may consider to using utf-8 as the target multi-byte character set.
then, you could use function WideCharToMultiByte for conversion where you can specifiy UTF-8 as target set.
Sara