iconv库可以实现编码格式转换,默认为Linux版本,如果要在windows下使用,需要在VC下重新编译
1. 下载 iconv 库 http://ftp.gnu.org/pub/gnu/libiconv/ 在这个地址下载想要的版本
2. VC6新建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
注:在VS2008中第2步注意
,可以先建立一个空工程,进入工程属性,常规->配置类型->静态库(.lib)
VC6编译的库不能在VS2008下用,要在VS2008重新编译
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////
下面是验证代码(复制自VLC):
#include <iconv.h> #include <stdio.h> #include <stdlib.h> #pragma comment(lib,"libIconv.lib") static char *dvbsi_to_gb2312( char*psz_instring, size_t i_length ) { char *psz_encoding, *psz_stringstart, *psz_outstring, *psz_tmp; char psz_encbuf[12]; size_t i_in, i_out; iconv_t iconv_handle; if( i_length < 1 ) return NULL; if( psz_instring[0] >= 0x20 ) { psz_stringstart = psz_instring; psz_encoding = "ISO_8859-1"; /* should be ISO6937 according tospec, but this seems to be the one used */ }else switch( psz_instring[0] ) { case 0x01: psz_stringstart = &psz_instring[1]; psz_encoding = "ISO_8859-5"; break; case 0x02: psz_stringstart = &psz_instring[1]; psz_encoding = "ISO_8859-6"; break; case 0x03: psz_stringstart = &psz_instring[1]; psz_encoding = "ISO_8859-7"; break; case 0x04: psz_stringstart = &psz_instring[1]; psz_encoding = "ISO_8859-8"; break; case 0x05: psz_stringstart = &psz_instring[1]; psz_encoding = "ISO_8859-9"; break; case 0x06: psz_stringstart = &psz_instring[1]; psz_encoding = "ISO_8859-10"; break; case 0x07: psz_stringstart = &psz_instring[1]; psz_encoding = "ISO_8859-11"; break; case 0x08: psz_stringstart = &psz_instring[1]; /*possibly reserved?*/ psz_encoding = "ISO_8859-12"; break; case 0x09: psz_stringstart = &psz_instring[1]; psz_encoding = "ISO_8859-13"; break; case 0x0a: psz_stringstart = &psz_instring[1]; psz_encoding = "ISO_8859-14"; break; case 0x0b: psz_stringstart = &psz_instring[1]; psz_encoding = "ISO_8859-15"; break; // case 0x10: // if( i_length < 3 ||psz_instring[1] != '\0' || psz_instring [2] > 0x0f // || psz_instring[2]== 0 ) // returnEnsureUTF8(strndup(psz_instring,i_length)); // sprintf( psz_encbuf,"ISO_8859-%d", psz_instring[2] ); // psz_stringstart =&psz_instring[3]; // psz_encoding =psz_encbuf; // break; case 0x11: psz_stringstart = &psz_instring[1]; psz_encoding = "UTF-16"; break; case 0x12: psz_stringstart = &psz_instring[1]; psz_encoding = "KSC5601-1987"; break; case 0x13: psz_stringstart = &psz_instring[1]; psz_encoding = "GB2312";/*GB-2312-1980 */ break; case 0x14: psz_stringstart = &psz_instring[1]; psz_encoding = "BIG-5"; break; // case 0x15: // returnEnsureUTF8(strndup(&psz_instring[1],i_length-1)); // break; default: /* invalid */ return NULL; // todo EnsureUTF8(strndup(psz_instring,i_length)); } iconv_handle = iconv_open( "GB2312", psz_encoding ); i_in = i_length - (psz_stringstart - psz_instring ); i_out = i_in * 6; psz_outstring = psz_tmp = (char*)malloc( i_out * sizeof(char) + 1 ); iconv( iconv_handle, (const char **)&psz_stringstart, &i_in,&psz_tmp, & i_out ); iconv_close( iconv_handle ); *psz_tmp = '\0'; return psz_outstring; } int main() { char a[] = {0x11, 0x73, 0xAF, 0x74, 0x03, 0x65, 0xC5, 0x6E, 0x38, 0x00}; char *b = dvbsi_to_gb2312(a, sizeof(a)); printf("%s\r\n", b); return 0; }