^ title
Convert std::string to WCHAR ?
I am trying to do this:
std::string s;
WCHAR w = s;
Also what do you mean what platform i am using?
std::string multi("Test");
std::wstring unicode(multi.begin(), multi.end());
This is the easiest form of multibyte to unicode conversion I know, won't even throw a compiler warning, unlike some solutions like std::copy. Then you can access the single characters of the wstring to get your WCHAR. Though given the little information and SiCranes answer which makes me a bit unsure, so please don't hit me if I misunderstood the actual problem ;)
EDIT:
Well given your next post it seems like it, but you can't simple do WCHAR = wstring eigther, as you can't do char = string. You rather want to do something amongst those lines, right?
std::wstring unicode; // suppose you used the method mentioned above to convert from string towstring
WCHAR w[256] = unicode.c_str() // you can only assign a (w)string to a (w)char array, not a single (w)char
I don't understand, the above code it giving me an error.
basically what i want is something like this
WCHAR convert(std::string s)
{
WCHAR w;
//do whatever needs to be done
return w;
}
std::string s = "something";
WCHAR w = convert(s);
A string is a sequence of zero or potentially many characters. A WCHAR is exactly one character. You cannot just cram a string into a single character.
What is the actual problem you're trying to solve?
A string is a sequence of zero or potentially many characters. A WCHAR is exactly one character. You cannot just cram a string into a single character.
What is the actual problem you're trying to solve?
to pass a string in the second parameter here
WCHAR* filename = L"file path...";
D3DX11CreateShaderResourceViewFromFile(device, filename, NULL, NULL, &texture, NULL);
This snippet always worked for me:
std::wstring ToStringW( const std::string& strText )
{
std::wstring wstrResult;
wstrResult.resize( strText.length() );
typedef std::codecvt<wchar_t, char, mbstate_t> widecvt;
std::locale locGlob;
std::locale::global( locGlob );
const widecvt& cvt( std::use_facet<widecvt>( locGlob ) );
mbstate_t State;
const char* cTemp;
wchar_t* wTemp;
cvt.in( State,
&strText[0], &strText[0] + strText.length(), cTemp,
(wchar_t*)&wstrResult[0], &wstrResult[0] + wstrResult.length(), wTemp );
return wstrResult;
}
Then use .c_str() on the std::wstring to get a WCHAR* like.
Here is one nice small snippet.
const wchar_t* to_wide( const std::string& strToConvert ) {
return std::wstring( strToConvert.begin(), strToConvert.end() ).c_str();
}
Notice: not working, skip it. I just leave it as is, to be here as warning how NOT to do some things.
This code is deeply broken. You're returning a pointer to memory that is freed when the function exits (because the temporary wstring will be destructed). It may seem to work, but that's the horrifying reality of undefined behavior - it often seems to work until it bites you viciously.Here is one nice small snippet.
const wchar_t* to_wide( const std::string& strToConvert ) { return std::wstring( strToConvert.begin(), strToConvert.end() ).c_str(); }