将const char *转换为const wchar_t * [英] Convert const char* to const wchar_t*
问题描述
我试图创建一个程序与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:
- char* to const wchar_t * conversion
- conversion from unsigned char* to const wchar_t*
平台无关方法发布在 http://ubuntuforums.org/showthread.php?t=1579640。此网站的来源(我希望我没有违反任何版权):
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 ;
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_t *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!