Convert const char* to const wchar_t*
There are multiple questions on SO that address the problem on Windows. Sample posts:
- char* to const wchar_t * conversion
- conversion from unsigned char* to const wchar_t*
There is a platform agnostic method posted at http://ubuntuforums.org/showthread.php?t=1579640. The source from this site is (I hope I am not violating any copyright):
#include <locale>
#include <iostream>
#include <string>
#include <sstream>
using namespace std ;
wstring widen( const string& str )
{
wostringstream wstm ;
const ctype<wchar_t>& ctfacet = use_facet<ctype<wchar_t>>(wstm.getloc()) ;
for( size_t i=0 ; i<str.size() ; ++i )
wstm << ctfacet.widen( str[i] ) ;
return wstm.str() ;
}
string narrow( const wstring& str )
{
ostringstream stm ;
// Incorrect code from the link
// const ctype<char>& ctfacet = use_facet<ctype<char>>(stm.getloc());
// Correct code.
const ctype<wchar_t>& ctfacet = use_facet<ctype<wchar_t>>(stm.getloc());
for( size_t i=0 ; i<str.size() ; ++i )
stm << ctfacet.narrow( str[i], 0 ) ;
return stm.str() ;
}
int main()
{
{
const char* cstr = "abcdefghijkl" ;
const wchar_t* wcstr = widen(cstr).c_str() ;
wcout << wcstr << L'\n' ;
}
{
const wchar_t* wcstr = L"mnopqrstuvwx" ;
const char* cstr = narrow(wcstr).c_str() ;
cout << cstr << '\n' ;
}
}
You can use mbstowcs:
wchar_t WBuf[100];
mbstowcs(WBuf,lua_tostring( /*...*/ ),99);
or more safe:
const char* sz = lua_tostring(/*...*/);
std::vector<wchar_t> vec;
size_t len = strlen(sz);
vec.resize(len+1);
mbstowcs(&vec[0],sz,len);
const wchar_t* wsz = &vec[0];