Libiconv库可以实现文字格式转换,VLC在DVB的SI信息中,字符(如SDT节目名字、提供商,和EIT等)解析显示用到这个库
1.1. 下载 iconv 库并解压,目前最新版本为 1.15
http://ftp.gnu.org/pub/gnu/libiconv/
2. VC2012新建win32静态库工程,libIconv。复制iconv库lib文件夹所有文件到工程文件夹下
3. 将config.h.in改名为config.h
4. 将libcharset/lib/localcharset.c复制过来
5. 将include文件夹下iconv.h.in复制过来,改名为iconv.h
6. 将库中scrLib/localcharset.h复制过来
7. 工程属性, C/C++, Preprocssor, Addtional include diretory增加当前文件夹”.”
8. 修改 iconv.h :
a. 删除所有的 @ 符号,
b. 注释掉/DLL_VARIABLE/,
c. ICONV_CONST改为const,
9. 修改localcharset.c:
注释掉 #include “configmake.h”
10. 把有main函数的.c文件删掉
生成libIconv.lib,头文件即iconv.h
本人编译好的LibIconv库,下载地址:https://download.csdn.net/download/xuebing1995/10538297
——————————————————————————————————————————————————
在需要的用到的时候直接将libIconv.lib和iconv.h考到工程里,然后加动态或者静态加进去。
#include"stdafx.h"
#include <iconv.h>
#include<string>
#include<iostream>
using namespace std;
int my_code_convert(char *from_charset,char *to_charset,char *inbuf,int inlen,char *outbuf,int outlen)
{
iconv_t cd;
char **pin=&inbuf;
char **pout=&outbuf;
cd=iconv_open(to_charset,from_charset);
if(cd==0)
{
iconv_close(cd);
return -1;
}
int ret=iconv(cd,(const char**)pin,(size_t*)&inlen,pout,(size_t*)&outlen);
if(ret==-1)
{
iconv_close(cd);
return -1;
}
iconv_close(cd);
return 0;
}
int main()
{
string strGbk = "啊啊啊啊1314 4FFFGg/*";
int num = strGbk.size();//获取两个字符数,也是我字所占的字节数
unsigned char* p = (unsigned char*)strGbk.c_str();
//string strUnicode = GbkToUnicode("啊啊1314 4FFFGg/*");//转成unicode编码
char outs[100]={0};
int outsLen=100;
int ret1=my_code_convert("gb2312", "UCS-2",(char*)p,num,outs,outsLen);
printf("outs=%s\n",outs);
char inbuf[]= {0x55,0x4a,0x55,0x4a,0x00}; //加上0x00字符串结束符,不会输出乱码
int inbufLen=strlen(inbuf);
char *outStr=new char[inbufLen*10];
memset(outStr,0,inbufLen*10);
int outStrLen=inbufLen*10;
//使用iconv库转换 USC-2 --->GB2312
int ret=my_code_convert("UCS-2","GB2312",inbuf,inbufLen,outStr,outStrLen);
if (ret!=0)
{
return 0;
}
cout<<outStr<<endl;
system("pause");
return 0;
}
运行结果如图: