I was trying to create a function that converts a string to a wstring, using the built-in function wsprintf.
For some reason, when I try to print the result wstring, nothing is printed, and when I try debugging, I saw it contains letters in Chinese (so I guess that the problem is that the sprintf function didn't add a leading zero-byte to each character for some reason, but I have no idea WHY).
Can someone please help me figure it out?
Here is what I did:
#include <iostream>
#include <string>
using std::wstring;
using std::string;
using std::wcout;
using std::endl;
wstring strToWstr(const string& str);
int main()
{
string str = "test";
wstring wstr = strToWstr(str);
wcout << wstr << endl;
}
wstring strToWstr(const string& str)
{
wchar_t* temp = NULL;
temp = new wchar_t[str.length() + 1]; //allocated string with the right length
swprintf(temp, str.length() + 1, L"%s", str.c_str());
wstring res = temp;
delete[] temp;
return res;
}