#ifndef __FONT_CHINA__
#define __FONT_CHINA__
class FontChina
{
public:
FontChina(void);
~FontChina(void);
static const char* G2U(const char* gb2312);
};
#endif //__FONT_CHINA__
#include "FontChina.h"
#include "cocos2d.h"
USING_NS_CC;
FontChina::FontChina(void)
{
}
FontChina::~FontChina(void)
{
}
const char* FontChina::G2U(const char* gb2312)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
int len = MultiByteToWideChar(0, 0, gb2312, -1, NULL, 0);
wchar_t* wstr = new wchar_t[len+1];
memset(wstr, 0, len+1);
MultiByteToWideChar(0, 0, gb2312, -1, wstr, len);
len = WideCharToMultiByte(65001, 0, wstr, -1, NULL, 0, NULL, NULL);
char* str = new char[len+1];
memset(str, 0, len+1);
WideCharToMultiByte(65001, 0, wstr, -1, str, len, NULL, NULL);
if(wstr) delete[] wstr;
return str;
#endif
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
return gb2312;
#endif
}
//第二种转换方式
static void UTF_8ToUnicode(wchar_t* pOut,char *pText);
static void GB2312ToUTF_8(string& pOut,char *pText, int pLen);
static void Gb2312ToUnicode(wchar_t* pOut,char *gbBuffer);
static void UnicodeToUTF_8(char* pOut,wchar_t* pText);void CommonResource::UTF_8ToUnicode(wchar_t* pOut,char *pText)
{
char* uchar = (char *)pOut;
uchar[1] = ((pText[0] & 0x0F) << 4) + ((pText[1] >> 2) & 0x0F);
uchar[0] = ((pText[1] & 0x03) << 6) + (pText[2] & 0x3F);
return;
}
void CommonResource::GB2312ToUTF_8(string& pOut,char *pText, int pLen)
{
char buf[4];
memset(buf,0,4);
pOut.clear();
int i = 0;
while(i < pLen)
{
if( pText[i] >= 0)
{
char asciistr[2]={0};
asciistr[0] = (pText[i++]);
pOut.append(asciistr);
}
else
{
wchar_t* pbuffer = new wchar_t[pLen+1];
Gb2312ToUnicode(pbuffer,pText+i);
UnicodeToUTF_8(buf,pbuffer);
pOut.append(buf);
i += 2;
delete []pbuffer;
}
}
return;
}
void CommonResource::Gb2312ToUnicode(wchar_t* pOut,char *gbBuffer)
{
size_t size = strlen(gbBuffer) + 1;
mbstowcs(pOut,gbBuffer,size);
return;
}
void CommonResource::UnicodeToUTF_8(char* pOut,wchar_t* pText)
{
char* pchar = (char *)pText;
pOut[0] = (0xE0 | ((pchar[1] & 0xF0) >> 4));
pOut[1] = (0x80 | ((pchar[1] & 0x0F) << 2)) + ((pchar[0] & 0xC0) >> 6);
pOut[2] = (0x80 | (pchar[0] & 0x3F));
return;
}读取txt文件内容如下:
#include <iostream>
#include "cocos2d.h"
using namespace cocos2d;
using namespace std;
/** 负责操作文件储存和读取
*/
class TDInvFileUtils {
public:
/** 读取本地文件,返回数据 */
static string getFileByName(string pFileName);
/** 储存内容到文件 */
static bool saveFile(char* pContent,string pFileName);
};
#include "TDInvFileUtils.h"
string TDInvFileUtils::getFileByName(string pFileName){
//第一先获取文件的路径
string path = CCFileUtils::sharedFileUtils()->getWritablePath() + pFileName;
CCLOG("path = %s",path.c_str());
//创建一个文件指针
FILE* file = fopen(path.c_str(), "r");
if (file) {
char* buf; //要获取的字符串
int len; //获取的长度
/*获取长度*/
fseek(file, 0, SEEK_END); //移到尾部
len = ftell(file); //提取长度
rewind(file); //回归原位
CCLOG("count the file content len = %d",len);
//分配buf空间
buf = (char*)malloc(sizeof(char) * len + 1);
if (!buf) {
CCLOG("malloc space is not enough.");
return NULL;
}
//读取文件
//读取进的buf,单位大小,长度,文件指针
int rLen = fread(buf, sizeof(char), len, file);
buf[rLen] = '\0';
CCLOG("has read Length = %d",rLen);
CCLOG("has read content = %s",buf);
string result = buf;
fclose(file);
free(buf);
return result;
}
else
CCLOG("open file error.");
return NULL;
}
bool TDInvFileUtils::saveFile(char *pContent, string pFileName){
//第一获取储存的文件路径
string path = CCFileUtils::sharedFileUtils()->getWritablePath() + pFileName;
CCLOG("wanna save file path = %s",path.c_str());
//创建一个文件指针
//路径、模式
FILE* file = fopen(path.c_str(), "w");
if (file) {
fputs(pContent, file);
fclose(file);
}
else
CCLOG("save file error.");
return false;
}
本文介绍了一个用于处理中文字符集转换的C++类FontChina,该类提供了从GB2312到Unicode的转换方法,并展示了两种不同的转换实现。此外,还提供了一个文件读写工具类TDInvFileUtils,用于读取和保存文件。
 GBK转UTF-8 以及 读取txt文件内容&spm=1001.2101.3001.5002&articleId=56284021&d=1&t=3&u=61fa40f9588a4b13a634dab943e3b4b8)
7000

被折叠的 条评论
为什么被折叠?



