将const char *转换为const wchar_t * [英] Convert const char* to const wchar_t*

查看:2302
本文介绍了将const char *转换为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:


  1. char * to const wchar_t * conversion

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

  1. char* to const wchar_t * conversion
  2. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆