#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转unicode,"UCS-2LE"代表unicode小端模式std::string GbkToUnicode(const std::string& strGbk)// 传入的strGbk是GBK编码 { return code_convert("gb2312", "UCS-2LE",strGbk);}int main() { ofstream out("b.txt"); unsigned char head[3] = {0xff,0xfe,0x00};//unicode文件头,0x00必须加上 out << head ; string str =GbkToUnicode("我") ;//"我"在vs文本编辑器里默认是gbk编码,需要通过GbkToUnicode转成Unicode编码 out << str; out.close(); return 0;}参照博客:Qt写入unicode编码格式的文本
新闻热点
疑难解答