转码
来源:互联网 发布:淘宝上买车险可靠吗 编辑:程序博客网 时间:2024/06/13 02:29
#include <iconv.h>
/*code from one to another*/
int mailparser::MailParser::codeconv(char *from_code, char *to_code, char *from, char *to)
{
iconv_t cd;
char *tptr, *fptr;
size_t ileft, oleft, ret,convert_len=0,i_len;
tptr = fptr =NULL;
if(from == NULL || to == NULL) return -1;
//如果有BOM BOM是编码
if((from[0] == 0xff && from[1] == 0xfe) || (from[0] == 0xfe && from[1] == 0xff))
{
from += 2;
}
//UTF-8的BOM,有时也没有BOM是编码
if((from[0] == 0xef && from[1] == 0xbb && from[2] == 0xbf))
{
from += 3;
}
cd = iconv_open(( const char * )to_code, ( const char * )from_code );
if ( cd == ( iconv_t )-1 )
{
return -1;
}
i_len=ileft = strlen(from);
fptr = from;
for( ; ; )
{
tptr = to;
oleft = strlen( from ) * 4 + 1;
memset( to, 0x00, strlen( from ) * 4 + 1 );
ret = iconv( cd, &fptr, &ileft, &tptr, &oleft );
convert_len+=ret;
if(ret==0)
{
break;
}
else if(ret>0 && ileft==0 && convert_len<i_len)
{
( void )iconv_close( cd );
return -1;
}
if( errno == EINVAL )
{
( void )iconv_close( cd );
return 1;
}
else if( errno == E2BIG )
{
continue;
}
else if( errno == EILSEQ )
{
( void )iconv_close( cd );
return 1;
}
else if( errno == EBADF )
{
( void )iconv_close( cd );
return -1;
}
else
{
( void )iconv_close( cd );
return -1;
}
}
( void )iconv_close( cd );
return 0;
}
String mailparser::MailParser::any2utf8(const String &src)
{
char code_knowned[13][20] = {'\0'} ;
int i, ret, code_knowned_num;
char *to = NULL;
strcpy(code_knowned[0],"UTF8");
strcpy(code_knowned[1],"GB2312");
strcpy(code_knowned[2],"GBK");
strcpy(code_knowned[3],"GB18030");
strcpy(code_knowned[4],"BIG5");
strcpy(code_knowned[5],"ISO646-KR");
strcpy(code_knowned[6],"EUC-KR");
strcpy(code_knowned[7],"EUC_JP");
strcpy(code_knowned[8],"ISO-2022-JP");
strcpy(code_knowned[9],"ISO-8859-1");
strcpy(code_knowned[10],"ISO-8859-2");
strcpy(code_knowned[11],"ISO-8859-9");
strcpy(code_knowned[12],"Shift_JIS");
if(src.size() < 1)
return src;
int len = src.size();
if(( to = ( char * )malloc( len * 4 + 1 )) == NULL )
{
return src;
}
memset( to, 0x00, len * 4 + 1 );
code_knowned_num = sizeof( code_knowned ) / sizeof( code_knowned[ 0 ] );
for( i = 0; i < code_knowned_num; i++ )
{
ret = codeconv( code_knowned[ i ], "UTF8", const_cast<char *>(src.c_str()), to );
if( ret == 0 )
{
String rstString = to;
if(to!=NULL)
{
free(to);
to = NULL;
}
return rstString;
}
else if( ret == 1 )
{
memset( to, 0x00, len * 4 + 1 );
continue;
}
else
{
memset( to, 0x00, len * 4 + 1 );
continue;
}
}
if(to!=NULL)
{
free(to);
to = NULL;
}
return src;
}
String mailparser::MailParser::any2utf8(const String &src, const String fromEncode)
{
int ret;
char *to = NULL;
if(src.size() < 1)
return src;
if(fromEncode.size()<1)
return src;
int len = src.size();
if(( to = ( char * )malloc( len * 4 + 1 )) == NULL )
{
return src;
}
memset( to, 0x00, len * 4 + 1 );
if((codeconv( const_cast<char *>(fromEncode.c_str()), "UTF8", const_cast<char *>(src.c_str()), to ))==0)
{
String rstString = to;
if(to!=NULL)
{
free(to);
to=NULL;
}
return rstString;
}
if(to!=NULL)
{
free(to);
to=NULL;
}
return src;
}
/*code from one to another*/
int mailparser::MailParser::codeconv(char *from_code, char *to_code, char *from, char *to)
{
iconv_t cd;
char *tptr, *fptr;
size_t ileft, oleft, ret,convert_len=0,i_len;
tptr = fptr =NULL;
if(from == NULL || to == NULL) return -1;
//如果有BOM BOM是编码
if((from[0] == 0xff && from[1] == 0xfe) || (from[0] == 0xfe && from[1] == 0xff))
{
from += 2;
}
//UTF-8的BOM,有时也没有BOM是编码
if((from[0] == 0xef && from[1] == 0xbb && from[2] == 0xbf))
{
from += 3;
}
cd = iconv_open(( const char * )to_code, ( const char * )from_code );
if ( cd == ( iconv_t )-1 )
{
return -1;
}
i_len=ileft = strlen(from);
fptr = from;
for( ; ; )
{
tptr = to;
oleft = strlen( from ) * 4 + 1;
memset( to, 0x00, strlen( from ) * 4 + 1 );
ret = iconv( cd, &fptr, &ileft, &tptr, &oleft );
convert_len+=ret;
if(ret==0)
{
break;
}
else if(ret>0 && ileft==0 && convert_len<i_len)
{
( void )iconv_close( cd );
return -1;
}
if( errno == EINVAL )
{
( void )iconv_close( cd );
return 1;
}
else if( errno == E2BIG )
{
continue;
}
else if( errno == EILSEQ )
{
( void )iconv_close( cd );
return 1;
}
else if( errno == EBADF )
{
( void )iconv_close( cd );
return -1;
}
else
{
( void )iconv_close( cd );
return -1;
}
}
( void )iconv_close( cd );
return 0;
}
String mailparser::MailParser::any2utf8(const String &src)
{
char code_knowned[13][20] = {'\0'} ;
int i, ret, code_knowned_num;
char *to = NULL;
strcpy(code_knowned[0],"UTF8");
strcpy(code_knowned[1],"GB2312");
strcpy(code_knowned[2],"GBK");
strcpy(code_knowned[3],"GB18030");
strcpy(code_knowned[4],"BIG5");
strcpy(code_knowned[5],"ISO646-KR");
strcpy(code_knowned[6],"EUC-KR");
strcpy(code_knowned[7],"EUC_JP");
strcpy(code_knowned[8],"ISO-2022-JP");
strcpy(code_knowned[9],"ISO-8859-1");
strcpy(code_knowned[10],"ISO-8859-2");
strcpy(code_knowned[11],"ISO-8859-9");
strcpy(code_knowned[12],"Shift_JIS");
if(src.size() < 1)
return src;
int len = src.size();
if(( to = ( char * )malloc( len * 4 + 1 )) == NULL )
{
return src;
}
memset( to, 0x00, len * 4 + 1 );
code_knowned_num = sizeof( code_knowned ) / sizeof( code_knowned[ 0 ] );
for( i = 0; i < code_knowned_num; i++ )
{
ret = codeconv( code_knowned[ i ], "UTF8", const_cast<char *>(src.c_str()), to );
if( ret == 0 )
{
String rstString = to;
if(to!=NULL)
{
free(to);
to = NULL;
}
return rstString;
}
else if( ret == 1 )
{
memset( to, 0x00, len * 4 + 1 );
continue;
}
else
{
memset( to, 0x00, len * 4 + 1 );
continue;
}
}
if(to!=NULL)
{
free(to);
to = NULL;
}
return src;
}
String mailparser::MailParser::any2utf8(const String &src, const String fromEncode)
{
int ret;
char *to = NULL;
if(src.size() < 1)
return src;
if(fromEncode.size()<1)
return src;
int len = src.size();
if(( to = ( char * )malloc( len * 4 + 1 )) == NULL )
{
return src;
}
memset( to, 0x00, len * 4 + 1 );
if((codeconv( const_cast<char *>(fromEncode.c_str()), "UTF8", const_cast<char *>(src.c_str()), to ))==0)
{
String rstString = to;
if(to!=NULL)
{
free(to);
to=NULL;
}
return rstString;
}
if(to!=NULL)
{
free(to);
to=NULL;
}
return src;
}
- 转码
- 转码
- 转码
- 转码
- 转码
- 转码
- 转码
- 转码
- java转码
- java转码
- php 转码
- url转码
- native2ascii转码
- C#转码
- Unicode转码
- PHP 转码
- URL转码
- url转码
- Sqlite内存数据库在多线程下的使用问题
- 自建简单的依赖注入容器Ioc Container
- 快乐面对人生的6个小技巧
- SQLite多线程写锁文件解决方案
- Android.mk文件语法规范
- 转码
- 结构体里的元素前面有一点“.”代表什么意思--结构体成员的初始化
- 海量数据处理
- Pentaho技术白皮书中文版(五)--集成 BIRT 和 Jasper
- 创业皆勤俭:省钱14大绝招 创造性的融资方式
- XQuery基本介绍
- 如何在字符串里查找第一个不重复的字母,即只出现一次的最靠前的字母
- des/3des加密解密验证网站
- Pentaho技术白皮书(六)--本地化指南