cocos2dx中UTF-8和GBK2312之间的转换
来源:互联网 发布:产品经理和数据分析 编辑:程序博客网 时间:2024/03/29 01:56
#ifndef _CHARSET_CONVERT_H_#define _CHARSET_CONVERT_H_//starios alter#include <stdlib.h>#include "stddef.h"#include <string>using namespace std;class CharsetConvert{public:static std::string GBKToUTF8(const char* msg); static std::string UTF8ToGBK(const char* msg);static int code_convert(const char *from_charset, const char *to_charset, const char *inbuf, size_t inlen, char *outbuf, size_t outlen);static int IsTextUTF8(const char* str,int length);};#endif
.cpp中
#include "CharsetConvert.h"#include <string>#if(CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) #include "external/win32-specific/icon/include/iconv.h"#else #include "iconv/include/iconv.h""#endifint CharsetConvert::IsTextUTF8(const char* str,int length){int i;int nBytes=0;//UFT8可用1-6个字节编码,ASCII用一个字节char chr;bool bAllAscii=true; //如果全部都是ASCII, 说明不是UTF-8for(i=0;i<length;i++){chr= *(str+i);if( (chr&0x80) != 0 ) // 判断是否ASCII编码,如果不是,说明有可能是UTF-8,ASCII用7位编码,但用一个字节存,最高位标记为0,o0xxxxxxxbAllAscii= false;if(nBytes==0) //如果不是ASCII码,应该是多字节符,计算字节数{if(chr>=0x80){if(chr>=0xFC&&chr<=0xFD)nBytes=6;else if(chr>=0xF8)nBytes=5;else if(chr>=0xF0)nBytes=4;else if(chr>=0xE0)nBytes=3;else if(chr>=0xC0)nBytes=2;else{return false;}nBytes--;}}else //多字节符的非首字节,应为 10xxxxxx{if( (chr&0xC0) != 0x80 ){return false;}nBytes--;}}if( nBytes > 0 ) //违返规则{return false;}if( bAllAscii ) //如果全部都是ASCII, 说明不是UTF-8{return false;}return true;}int CharsetConvert::code_convert(const char *from_charset, const char *to_charset, const char *inbuf, size_t inlen, char *outbuf, size_t outlen){iconv_t cd;const char *temp = inbuf;const char **pin = &temp;char **pout = &outbuf;memset(outbuf,0,outlen);cd = iconv_open(to_charset,from_charset);if(cd==0) return -1;if(iconv(cd,pin,&inlen,pout,&outlen)==-1) return -1;iconv_close(cd);return 0;} std::string CharsetConvert::GBKToUTF8(const char *inbuf){size_t inlen = strlen(inbuf);char * outbuf = new char[inlen * 2 + 2];std::string strRet;if(code_convert("gb2312", "utf-8", inbuf, inlen, outbuf, inlen * 2 + 2) == 0){strRet = outbuf;}delete [] outbuf;return strRet;}std::string CharsetConvert::UTF8ToGBK(const char *inbuf){size_t inlen = strlen(inbuf);char * outbuf = new char[inlen * 2 + 2];std::string strRet;if(code_convert("utf-8", "gb2312", inbuf, inlen, outbuf, inlen * 2 + 2) == 0){strRet = outbuf;}delete [] outbuf;return strRet;}
0 0
- cocos2dx中UTF-8和GBK2312之间的转换
- vs2005中GB2312字符串和UTF-8之间的转换
- C++中GB2312字符串和UTF-8之间的转换
- C++中GB2312字符串和UTF-8之间的转换
- vc++中GB2312字符串和UTF-8之间的转换
- C++中GB2312字符串和UTF-8之间的转换
- C++中GB2312字符串和UTF-8之间的转换
- C++中GB2312字符串和UTF-8之间的转换
- C++中GB2312字符串和UTF-8之间的转换
- C++中GB2312字符串和UTF-8之间的转换
- UTF-8、GBK、GBK2312等字符编码的区别和vim乱码等相关问题研究。
- Unicode和UTF-8之间的转换
- unicode和utf-8之间的转换
- UTF-8和Unicode之间的转换!
- (UTF-8 /GBK/Unicode/GBK2312)
- Cocos2dx学习: GBK 和 UTF-8的转换
- C++中GB2312字符串和UTF-8之间的转换 从CString 的转换
- C++ UTF-8 转换助手 实现UTF-8 和UTF-16之间的转换
- java中常犯的几个错误,自勉。
- 第六周项目(4):数制转换
- iGrimaceVX3.0和1.44在线源手机直接安装教程
- 简谈sort排序
- Java中的equals和hashCode方法详解
- cocos2dx中UTF-8和GBK2312之间的转换
- 关于VBA Excel开发中连接MySQL数据库的问题
- android 仿微信雷达扫描
- android switch模块
- C#学习之多线程开发技术(八)
- svn 图标所表示的意思
- 用SparseArray代替HashMap
- 一个简单的jsp+servlet实例,实现简单的登录
- c# 调用Microsoft XPS Document Writer打印机,将Pdf文件转换成Xps文件