ifstream讀取unicode文本到string時(shí),需要過(guò)濾文本開(kāi)始ff fe兩個(gè)字節(jié),否則轉(zhuǎn)成gbk會(huì)失敗。
ifstream讀取utf8文本到string時(shí),需要過(guò)濾文本開(kāi)始efbbbf三個(gè)字節(jié),否則轉(zhuǎn)成gbk會(huì)失敗。
下面是實(shí)現(xiàn)代碼:
#include <iostream>#include <string>#include <fstream>#include <iconv.h>using namespace std;#PRagma comment(lib,"libIconv.lib")//編碼轉(zhuǎn)換,source_charset是源編碼,to_charset是目標(biāo)編碼std::string code_convert(char *source_charset, char *to_charset, const std::string& sourceStr) //sourceStr是源編碼字符串{ iconv_t cd = iconv_open(to_charset, source_charset);//獲取轉(zhuǎn)換句柄,void*類型 if (cd == 0) return ""; size_t inlen = sourceStr.size(); size_t outlen = 255; char* inbuf = (char*)sourceStr.c_str(); char outbuf[255];//這里實(shí)在不知道需要多少個(gè)字節(jié),這是個(gè)問(wèn)題 //char *outbuf = new char[outlen]; 另外outbuf不能在堆上分配內(nèi)存,否則轉(zhuǎn)換失敗,猜測(cè)跟iconv函數(shù)有關(guān) memset(outbuf, 0, outlen); char *poutbuf = outbuf; //多加這個(gè)轉(zhuǎn)換是為了避免iconv這個(gè)函數(shù)出現(xiàn)char(*)[255]類型的實(shí)參與char**類型的形參不兼容 if (iconv(cd, &inbuf, &inlen, &poutbuf,&outlen) == -1) return ""; std::string strTemp(outbuf);//此時(shí)的strTemp為轉(zhuǎn)換編碼之后的字符串 iconv_close(cd); return strTemp;}//gbk轉(zhuǎn)UTF-8 std::string GbkToUtf8(const std::string& strGbk)// 傳入的strGbk是GBK編碼 { return code_convert("gb2312", "utf-8",strGbk);}//UTF-8轉(zhuǎn)gbkstd::string Utf8ToGbk(const std::string& strUtf8){ return code_convert("utf-8", "gb2312", strUtf8);}//gbk轉(zhuǎn)unicode,"UCS-2LE"代表unicode小端模式std::string GbkToUnicode(const std::string& strGbk)// 傳入的strGbk是GBK編碼 { return code_convert("gb2312", "UCS-2LE",strGbk);}//unicode轉(zhuǎn)gbkstd::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();//獲取兩個(gè)字符數(shù),也是我字所占的字節(jié)數(shù) 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"); //過(guò)濾文本開(kāi)始ff fe兩個(gè)字節(jié) char a; in>>a; in>>a; string strUnicode; in >> strUnicode; in.close(); cout<<UnicodeToGbk(strUnicode)<<endl;//轉(zhuǎn)成gbk輸出 num = strUnicode.size(); p = (unsigned char*)strUnicode.c_str(); for (int i = 0; i < num; i++) { printf("%0x", *p); p++; } //輸出1162 因?yàn)槟J(rèn)是小端模式,所以我的unicode編碼是0x6211 printf("/n"); //3、讀取"utf8.txt" in.open("utf8.txt"); //過(guò)濾文本開(kāi)始efbbbf三個(gè)字節(jié) char b; in>>b; in>>b; in>>b; string strUtf8; in>>strUtf8; in.close(); cout<<Utf8ToGbk(strUtf8)<<endl;//轉(zhuǎn)成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;}
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注