将const char*转换为const wchar_t*

Gia*_*e47 7 c++ irrlicht type-conversion

我正在尝试使用Irrlicht创建一个程序,该程序从Lua编写的配置文件加载某些东西,其中一个是窗口标题.但是,该lua_tostring函数返回一段const char*时间Irrlicht设备的方法setWindowCaption需要a const wchar_t*.如何转换返回的字符串lua_tostring

R S*_*ahu 6

在SO上有多个问题可以解决Windows上的问题.样本帖子:

  1. char*到const wchar_t*转换
  2. 从unsigned char*转换为const wchar_t*

http://ubuntuforums.org/showthread.php?t=1579640上发布了一个平台无关的方法.本网站的来源是(我希望我没有侵犯任何版权):

#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' ;
  } 
}
Run Code Online (Sandbox Code Playgroud)