Chinaunix首页 | 论坛 | 博客
  • 博客访问: 63332
  • 博文数量: 18
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 183
  • 用 户 组: 普通用户
  • 注册时间: 2014-01-21 18:29
文章分类
文章存档

2014年(18)

我的朋友

分类: C/C++

2014-03-03 17:36:04

//h
#pragma once
#include
using namespace std;
class CMultiLang
{
public:
  static string iconvGbkToUtf8(const string& str);//Gbk是GB2312的扩展集多字节编码,可以包含繁体、日文假名等字符。
  //用法示例
  //CCLabelTTF* pLabel = CCLabelTTF::create(CMultiLang::iconvGbkToUtf8("わだぅわ、你好cocos2d-x!").c_str(), "Arial", TITLE_FONT_SIZE);
  //pLabel->setColor(ccc3(0, 255, 0));
  //字符编码转换函数
  static bool iconvConvert(const char* from_charset, const char* to_charset, const char* inbuf, int inlen, char* outbuf, int outlen);
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    
//ccp
#include "MultiLang.h"
#include "iconv/iconv.h"
//字符编码转换函数
bool CMultiLang::iconvConvert(const char* from_charset, const char* to_charset, const char* inbuf, int inlen, char* outbuf, int outlen)
{
  iconv_t cd=iconv_open(to_charset, from_charset);
  if(cd==0)
    return false;
  const char** pin=&inbuf;
  char** pout=&outbuf;
  memset(outbuf, 0, outlen);
  size_t ret=iconv(cd, pin, (size_t*)&inlen, pout, (size_t*)&outlen);
  iconv_close(cd);
  return ret==(size_t)(-1)? false: true;
}
string CMultiLang::iconvGbkToUtf8(const string& str)
{
  const char* textIn=str.c_str();
  int inLen=str.length();
  int outLen=inLen*2+1;
  char* textOut=(char*)malloc(outLen);
     
  bool ret=false;
  if(textOut)
    ret=iconvConvert("gbk", "utf-8", textIn, inLen, textOut, outLen);
  string strOut=ret? string(textOut): string();
  free(textOut);
     
  return strOut;
}
阅读(2085) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~