• 13
• 15
• 19
• 27
• 9

# Convert std::string to WCHAR ?

This topic is 1759 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

^ title

##### Share on other sites
You might be able to use mbstowcs() to do so. If you gave more information, like what platform you are using then a more confident answer could be given.

##### Share on other sites

I am trying to do this:

std::string s;

WCHAR w = s;


Also what do you mean what platform i am using?

##### Share on other sites
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

Edited by Juliean

##### Share on other sites

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);


##### Share on other sites

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?

##### Share on other sites

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);


##### Share on other sites

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.

##### Share on other sites

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.

Edited by streamer