Nov 13, 2012 at 1:32am UTC
I want to convert my string to whcar_t.
I try this code but i can't success :S
string username="UserName";
wstring widestr = wstring(username.begin(), username.end());
wchar_t* widecstr = widestr.c_str();
Last edited on Nov 13, 2012 at 1:32am UTC
Nov 13, 2012 at 4:20am UTC
If your C++ compiler supports C99 features, you can do one without the temporary
std::wstring :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <cstring>
#include <iostream>
#include <string>
int main(void )
{
std::string username = "username" ;
const size_t len = username.length() + 1;
wchar_t wcstring[len];
swprintf(wcstring, len, L"%s" , username.c_str());
std::wcout << wcstring << std::endl;
return (0);
}
Obviously, you can still do it without C99 feature support by allocating the wchar_t array on the heap. Your mileage will vary.
Last edited on Nov 13, 2012 at 4:26am UTC