Skip to main content
GameDev.net gamedev.net
🔒 Locked

Convert std::string to WCHAR ?

Started by michaelmk86 May 20, 2013 at 11:18 PM 20 replies 38.5k views
SiCrane
SiCrane
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.
michaelmk86
michaelmk86

I am trying to do this:


std::string s;

WCHAR w = s;

Also what do you mean what platform i am using?

Juliean
Juliean

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
michaelmk86
michaelmk86

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

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?

michaelmk86
michaelmk86

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

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.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>
streamer
streamer

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.

ApochPiQ
ApochPiQ

Here is one nice small snippet.


const wchar_t* to_wide( const std::string& strToConvert ) {
  return std::wstring( strToConvert.begin(), strToConvert.end() ).c_str();
}
 


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.
Juliean
Juliean


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.

+1 for noticing this, I totally read over it to be honestly. Especially noteworth that this can, and will lead to many horrible things happening - for example, in my first resource cache class, I was storing the resources by LPCWSTR. It seemed to work fine while I was only hardcoding everything in the code, but after a while I got an issue where setting constant to my effects was only working sporadically. Turned out the LPCWSTR-keys got trashed, and whenever accessing an effect from the cache, a new isntance was loaded and stored in another key that became trashed, and so forward. Wouldn't even had thought about looking there in the first place, as it appeared to be a graphics-related error. Moral of the story: Always double-check on how you use your strings.

Eliad Moshe
Eliad Moshe

Here is my implementation:


		template <typename T>
			size_t SizeOfStr(T * str)
			{
				if(!str) return 0 ;
				size_t i = 0 ;
				while(str[i++]);
				return i ;
			}

		template <typename T , typename U >
			U * ConvertStr(T * t, size_t Size = 0)
			{
				if(!Size)
				{
					Size = SizeOfStr(t);
				}

				U * u = new U[Size]; 
				for(size_t i = 0 ; i < Size ; i++ ) u = U(t);
				return u ;
			}

Do not forget to clean resources once you are done.. smile.png

michaelmk86
michaelmk86

I solve my problem by doing the following:


std::wstring s2ws(const std::string& s)
{
	int len;
	int slength = (int)s.length() + 1;
	len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
	wchar_t* buf = new wchar_t[len];
	MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
	std::wstring r(buf);
	delete[] buf;
	return r;
}

#ifdef UNICODE
std::wstring stemp = s2ws(filename); // Temporary buffer is required
LPCTSTR L_filename = stemp.c_str();
#else
LPCTSTR L_filename = s.c_str();
#endif
	
D3DX11CreateShaderResourceViewFromFile(device, L_filename, NULL, NULL, &texture, NULL);

i just couldn't imagine such a simple thing to be that complicated.


Juliean
Juliean

i just couldn't imagine such a simple thing to be that complicated.

And you didn't do it as simple as:


std::wstring s2ws(const std::string& s)
{   
	return std::wstring(s.begin(), s.end());
}

#ifdef UNICODE
std::wstring stemp = s2ws(filename); // Temporary buffer is required
#else
std::Wstring stemp = s;
#endif
	
D3DX11CreateShaderResourceViewFromFile(device, stemp.c_str(), NULL, NULL, &texture, NULL);

like suggested because of... ?

kunos
kunos
behold the magic trick 1:

D3DX11CreateShaderResourceViewFromFileA(device, filename.c_str(), NULL, NULL, &texture, NULL);


Notice the final A? That will give you the (char*) version of the function as opposed to the WCHAR* version of it.

If you really want to use the WCHAR* .. the code is quite easy:

wstring l_filename(filename.begin(),filename.end());
D3DX11CreateShaderResourceViewFromFile(device, l_filename.c_str(), NULL, NULL, &texture, NULL);
streamer
streamer


Here is one nice small snippet.


const wchar_t* to_wide( const std::string& strToConvert ) {
  return std::wstring( strToConvert.begin(), strToConvert.end() ).c_str();
}
 
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.

You are right! :)

+1 for noticing it.

Bruno Sofiato
Bruno Sofiato

My C++ is a bit rusty ... but perhaps


std::wstring toWideString(std::string & str) {
  std::wstringstream ws;
  ws << str.c_str();
  return ws.str();
} 
Juliean
Juliean


My C++ is a bit rusty ... but perhaps

std::wstring toWideString(std::string & str) {
std::wstringstream ws;
ws << str.c_str();
return ws.str();
}

Should work, but is still more complicated than the wstring-ctor solution offered above many times.

Cornstalks
Cornstalks

Here is my implementation:


		template <typename T>
			size_t SizeOfStr(T * str)
			{
				if(!str) return 0 ;
				size_t i = 0 ;
				while(str[i++]);
				return i ;
			}

		template <typename T , typename U >
			U * ConvertStr(T * t, size_t Size = 0)
			{
				if(!Size)
				{
					Size = SizeOfStr(t);
				}

				U * u = new U[Size]; 
				for(size_t i = 0 ; i < Size ; i++ ) u = U(t);
				return u ;
			}

Do not forget to clean resources once you are done.. smile.png

Converting character code points doesn't work like that (at least for any code points requiring multiple bytes). If the input string is UTF-8 (and has any code points requiring more than one byte), that will not convert it right (For example, 0xC482 (UTF-8 "?") will be converted to 0x00C40082, which is not the right conversion).

MichaBen
MichaBen

If the input string is ASCII, you can easily expand the string using the methods given above. However if the input string is unicode, that won't work. In unicode a single character can consist of multiple code points which in turn can consist of multiple bytes. Typically you don't think in characters when encoding strings, but in code points. This is because unicode can combine characters from two code points, for example ' and e become é (although in this particular case there is a code point for é as well). This is also why you can never compare if two unicode strings are equal without normalizing them first. Unicode also has over a million code points (although not all are used yet), but a byte can only hold 256 and a 16-bit wchar only 65536. In order to be able to store all million code points, both UTF-8 and UTF-16 (although lots of people seem to forget this about the latter) encode code points in one or more pairs. For UTF-8 this means a single code point can take 1, 2, 3 or 4 bytes, for UTF-16 it's always 2 or 4. That means when converting, you first have to decode the code point, then encode it again, there is no direct convertion byte by byte possible.

You could do this yourself, it's not that hard and a good exercise to make a UTF-8 to UTF-16 converter. I would be useful if you start mixing the two to actually understand how it works, rather then to copy/paste a piece of code that may or may not work, or code that will later fail (the wstring constructor trick seems to fail horribly if your string is non-ascii). Or lookup boost, it has some conversion stuff in it as well.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.