GB2312转UTF8示例程序
2013-04-28 14:13
239 查看
#include <Windows.h>
#include <stdio.h>
#include <string>
bool GB2312ToUTF8(const char *pGb2312,std::string &strUtf8)
{
//GB2312 to unicode
int nUniCodeLen = MultiByteToWideChar(CP_ACP,0,pGb2312,-1,NULL,0);
wchar_t *pcUnicode = new wchar_t[nUniCodeLen + 1];
memset(pcUnicode,0,nUniCodeLen * 2 +2);
MultiByteToWideChar(CP_ACP,0,pGb2312,-1,pcUnicode,nUniCodeLen);
//unicode to utf8
int nUtf8Len = WideCharToMultiByte(CP_UTF8,0,pcUnicode,-1,NULL,0,NULL,NULL);
char *pcUtf8 = new char[nUtf8Len + 1];
memset(pcUtf8,0,nUtf8Len +1 );
WideCharToMultiByte(CP_UTF8,0,pcUnicode,-1,pcUtf8,nUtf8Len,NULL,NULL);
strUtf8 = pcUtf8;
delete []pcUtf8;
delete []pcUnicode;
return true;
}
std::string getContentsFromXmlFile(std::string &fileName)
{
FILE *fp = NULL;
if((fp = fopen(fileName.c_str(),"rb")) == NULL)
{
printf("file: %s open error ! ",fileName.c_str());
std::string sTmp;
return sTmp;
}
fseek(fp,0,SEEK_SET);
fseek(fp,0,SEEK_END);
int fileSize = ftell(fp) + 1;
if(!(fileSize - 1 > 5))
{
std::string temp("");
fclose(fp);
return temp;
}
char *pchBuffer = new char[fileSize];
fseek(fp,0,SEEK_SET);
memset(pchBuffer,0,fileSize);
fread(pchBuffer,fileSize,1,fp);
pchBuffer[fileSize - 1] = '\0';
std::string fileContent(pchBuffer);
if(pchBuffer != NULL)
{
delete []pchBuffer;
pchBuffer = NULL;
}
fclose(fp);
return fileContent;
}
std::string getContentsFromXmlFile(const char *filename)
{
std::string strFileName(filename);
return getContentsFromXmlFile(strFileName);
}
int main(int argc,char **argv)
{
std::string strXmlContent = getContentsFromXmlFile("fileName.xml");
std::string strResult;
GB2312ToUTF8(strXmlContent.c_str(),strResult);
FILE *fp = fopen("result.xml","ab");
fwrite(strResult.c_str(),strResult.length(),1,fp);
fclose(fp);
return 0;
}
#include <stdio.h>
#include <string>
bool GB2312ToUTF8(const char *pGb2312,std::string &strUtf8)
{
//GB2312 to unicode
int nUniCodeLen = MultiByteToWideChar(CP_ACP,0,pGb2312,-1,NULL,0);
wchar_t *pcUnicode = new wchar_t[nUniCodeLen + 1];
memset(pcUnicode,0,nUniCodeLen * 2 +2);
MultiByteToWideChar(CP_ACP,0,pGb2312,-1,pcUnicode,nUniCodeLen);
//unicode to utf8
int nUtf8Len = WideCharToMultiByte(CP_UTF8,0,pcUnicode,-1,NULL,0,NULL,NULL);
char *pcUtf8 = new char[nUtf8Len + 1];
memset(pcUtf8,0,nUtf8Len +1 );
WideCharToMultiByte(CP_UTF8,0,pcUnicode,-1,pcUtf8,nUtf8Len,NULL,NULL);
strUtf8 = pcUtf8;
delete []pcUtf8;
delete []pcUnicode;
return true;
}
std::string getContentsFromXmlFile(std::string &fileName)
{
FILE *fp = NULL;
if((fp = fopen(fileName.c_str(),"rb")) == NULL)
{
printf("file: %s open error ! ",fileName.c_str());
std::string sTmp;
return sTmp;
}
fseek(fp,0,SEEK_SET);
fseek(fp,0,SEEK_END);
int fileSize = ftell(fp) + 1;
if(!(fileSize - 1 > 5))
{
std::string temp("");
fclose(fp);
return temp;
}
char *pchBuffer = new char[fileSize];
fseek(fp,0,SEEK_SET);
memset(pchBuffer,0,fileSize);
fread(pchBuffer,fileSize,1,fp);
pchBuffer[fileSize - 1] = '\0';
std::string fileContent(pchBuffer);
if(pchBuffer != NULL)
{
delete []pchBuffer;
pchBuffer = NULL;
}
fclose(fp);
return fileContent;
}
std::string getContentsFromXmlFile(const char *filename)
{
std::string strFileName(filename);
return getContentsFromXmlFile(strFileName);
}
int main(int argc,char **argv)
{
std::string strXmlContent = getContentsFromXmlFile("fileName.xml");
std::string strResult;
GB2312ToUTF8(strXmlContent.c_str(),strResult);
FILE *fp = fopen("result.xml","ab");
fwrite(strResult.c_str(),strResult.length(),1,fp);
fclose(fp);
return 0;
}
相关文章推荐
- php自定义中文字符串截取函数substr_for_gb2312及substr_for_utf8示例
- php自定义中文字符串截取函数substr_for_gb2312及substr_for_utf8示例
- Grails示例程序-用代码实现将文件压缩后下载
- 小程序视频或音频自定义可拖拽进度条的示例代码
- spread 7.0 基础知识、程序示例、设置CheckBox、image 格式
- UTF8 编码方式,以及GB2312、Unicode、UCS等编码介绍。
- 解决Java程序使用MySQL时返回参数为乱码的示例教程
- GPU编程之CUDA(九)——示例程序运行截图【5_Simulations】
- 使用Winpcap示例程序的时候,会出现很多错误(转)
- Hadoop学习笔记(一)之示例程序:计算每年的最高温度MaxTemperature
- java小程序 示例
- 解决BCG库示例程序中的一个诡异编译错误
- 创建ArcGIS API for JavaScript的第一个示例程序
- 爬虫示例程序
- OWA或Messenger样式的信息提示窗口——编写ASP.NET AJAX Extender控件(下):使用该控件(示例程序)
- Socket编程指南及示例程序
- 结合SLAM十四讲的示例程序理解SE3, se(3), so(3),R, t等
- 均值滤波blur函数的使用示例程序
- spring程序示例(SpringDemo项目)
- nginx subrequest演示示例程序