将const char *转换为const wchar

编程入门 行业动态 更新时间:2024-10-23 16:26:52
本文介绍了将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:

  • char * to const wchar_t * conversion
  • 从unsigned char *转换为const wchar_t *
  • char* to const wchar_t * conversion
  • conversion from unsigned char* to const wchar_t*
  • 平台无关方法发布在 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

    本文发布于:2023-10-07 14:31:00,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1469637.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:转换为   const   char   wchar

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!