游戏开发论坛

 找回密码
 立即注册
搜索
查看: 3756|回复: 2

老题重谈,怎样把一个CEGUI::String转化成std::string

[复制链接]

1

主题

2

帖子

6

积分

新手上路

Rank: 1

积分
6
发表于 2009-5-26 10:56:00 | 显示全部楼层 |阅读模式
CEGUI控件上面有中文字符串,得到控件的文本时CEGUI的字符串
我想把这个CEGUI的字符串转化成普通的字符串并且中文部分不是乱码
怎么吧,本人是菜鸟,希望有高手回答,谢谢了 [em3] [em3]

11

主题

190

帖子

255

积分

中级会员

Rank: 3Rank: 3

积分
255
发表于 2009-5-26 14:05:00 | 显示全部楼层

Re:老题重谈,怎样把一个CEGUI::String转化成std::string

CEGUI::String内部是utf32编码,
先用CEGUI::String::c_str()可以获得utf8编码格式,
然后用Windows的MultiByteToWideChar转换成utf16格式,如果想得到普通的gbk编码,在用WideCharToMultiByte转换成本地编码

-----------------
欢迎光临我的博客 http://www.thecodeway.com

32

主题

1259

帖子

1351

积分

金牌会员

Rank: 6Rank: 6

积分
1351
发表于 2009-5-26 14:35:00 | 显示全部楼层

Re: 老题重谈,怎样把一个CEGUI::String转化成std::string


  1. std::string& decode(const CEGUI::String& str)
  2. {
  3.         char sdes[2048];
  4.         static std::string result;

  5.         int len = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
  6.         wchar_t* wcstr = new wchar_t[len];
  7.         MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, wcstr, len);

  8.         int wlen = WideCharToMultiByte(CP_ACP, 0, wcstr, -1, NULL, 0, NULL, NULL);
  9.         char* nstr = new char[wlen];
  10.         WideCharToMultiByte(CP_ACP, 0, wcstr, -1, nstr, wlen, NULL, NULL);

  11.         strcpy(sdes, nstr);

  12.         delete[] wcstr;
  13.         delete[] nstr;

  14.         result = sdes;

  15.         return result;
  16. }
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-12-20 04:40

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表