1

To convert a string literal to wchar_t we can use L like:

wchar_t variable[10] = L"some text";`

But if the string is stored inside a variable then how do I convert it to wchar_t?

Suppose the string is in a variable

string varString="someText";

I want to store it in a variable of type wchar_t, for example wchar_t var;

How do I type cast and store it?

I want to place the variables inside a loop where their values will change with each cycle:

for(i=0;i<10;i++)
{  
   var=(*some kind of casting*)varString;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Raaj
  • 47
  • 1
  • 6

1 Answers1

1

Two different ways...depending on simplicity... This is in Visual C++ group...

So first I would try using CStringW. Depending on #defines your regular CString might be a CStringA or CStringW. But, you can say CStringW.

CStringW sWide = "abcdef"; // uses current thread code page
const wchar_t* pWide = sWide.GetString(); // pointer only valid for scope of sWide

Or you can use MultiByteToWideChar() API.

wchar_t wszBuf[512];
MultiByteToWideChar(CP_ACP, 0, "abcdef", 6, wszBuf, _countof(wszBuf)); // substitute "abcdef" and the 6 (length) for your usage...
Joseph Willcoxson
  • 5,853
  • 1
  • 15
  • 29