您的位置:首页 > 其它

ifStream读取ANSI/Unicode/Utf8文本保存到string里并转成gbk编码

2017-02-03 22:04 731 查看
ifstream读取unicode文本到string时,需要过滤文本开始ff fe两个字节,否则转成gbk会失败。

ifstream读取utf8文本到string时,需要过滤文本开始efbbbf三个字节,否则转成gbk会失败。

下面是实现代码:

#include <iostream>
#include <string>
#include <fstream>
#include <iconv.h>
using namespace std;

#pragma comment(lib,"libIconv.lib")

//编码转换,source_charset是源编码,to_charset是目标编码
std::string code_convert(char *source_charset, char *to_charset, const std::string& sourceStr) //sourceStr是源编码字符串
{
iconv_t cd = iconv_open(to_charset, source_charset);//获取转换句柄,void*类型
if (cd == 0)
return "";

size_t inlen = sourceStr.size();
size_t outlen = 255;
char* inbuf = (char*)sourceStr.c_str();
char outbuf[255];//这里实在不知道需要多少个字节,这是个问题
//char *outbuf = new char[outlen]; 另外outbuf不能在堆上分配内存,否则转换失败,猜测跟iconv函数有关
memset(outbuf, 0, outlen);

char *poutbuf = outbuf; //多加这个转换是为了避免iconv这个函数出现char(*)[255]类型的实参与char**类型的形参不兼容
if (iconv(cd, &inbuf, &inlen, &poutbuf,&outlen) == -1)
return "";

std::string strTemp(outbuf);//此时的strTemp为转换编码之后的字符串
iconv_close(cd);
return strTemp;
}

//gbk转UTF-8
std::string GbkToUtf8(const std::string& strGbk)// 传入的strGbk是GBK编码
{
return code_convert("gb2312", "utf-8",strGbk);
}

//UTF-8转gbk
std::string Utf8ToGbk(const std::string& strUtf8)
{
return code_convert("utf-8", "gb2312", strUtf8);
}

//gbk转unicode,"UCS-2LE"代表unicode小端模式
std::string GbkToUnicode(const std::string& strGbk)// 传入的strGbk是GBK编码
{
return code_convert("gb2312", "UCS-2LE",strGbk);
}

//unicode转gbk
std::string UnicodeToGbk(const std::string& strGbk)// 传入的strGbk是GBK编码
{
return code_convert("UCS-2LE", "gb2312",strGbk);
}

int main()
{
//1、读取"ANSI.txt"
ifstream in("ANSI.txt");
string strGbk;
in>>strGbk;
in.close();
cout<<strGbk<<endl;

int num = strGbk.size();//获取两个字符数,也是我字所占的字节数
unsigned char* p = (unsigned char*)strGbk.c_str();
for (int i = 0; i < num; i++)
{
printf("%0x", *p);
p++;
} //输出ced2 所以我的GBK编码是0xced2
printf("\n");

//2、读取"unicode.txt"
in.open("unicode.txt");
//过滤文本开始ff fe两个字节
char a;
in>>a;
in>>a;
string strUnicode;
in >> strUnicode;
in.close();
cout<<UnicodeToGbk(strUnicode)<<endl;//转成gbk输出

num = strUnicode.size();
p = (unsigned char*)strUnicode.c_str();
for (int i = 0; i < num; i++)
{
printf("%0x", *p);
p++;
} //输出1162 因为默认是小端模式,所以我的unicode编码是0x6211
printf("\n");

//3、读取"utf8.txt"
in.open("utf8.txt");
//过滤文本开始efbbbf三个字节
char b;
in>>b;
in>>b;
in>>b;
string strUtf8;
in>>strUtf8;
in.close();
cout<<Utf8ToGbk(strUtf8)<<endl;//转成gbk输出

num = strUtf8.size();
p = (unsigned char*)strUtf8.c_str();
for (int i = 0; i < num; i++)
{
printf("%0x", *p);
p++;
} //输出e68891
printf("\n");

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: