我试图创建一个程序与Irrlicht加载某些东西从一个配置文件中写的Lua,其中之一是窗口标题。然而, lua_tostring 函数返回一个 const char * ,而Irrlicht设备的方法 setWindowCaption 期望一个 const wchar_t * 。如何转换 lua_tostring ?返回的字符串
I am trying to create a program with Irrlicht that loads certain things from a configuration file written in Lua, one of which is the window title. However, the lua_tostring function returns a const char* while the Irrlicht device's method setWindowCaption expects a const wchar_t*. How can I convert the string returned by lua_tostring?
推荐答案多个问题,以解决Windows上的问题。示例信息:
There are multiple questions on SO that address the problem on Windows. Sample posts:
平台无关方法发布在 ubuntuforums/showthread.php?t=1579640。此网站的来源(我希望我没有违反任何版权):
There is a platform agnostic method posted at ubuntuforums/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 ; const ctype<char>& ctfacet = use_facet< ctype<char> >( 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' ; } }更多推荐
将const char *转换为const wchar
发布评论