/******************************************************
EncodingConv.c
使用iconv进行字符编码转换
SUNLAN
2006/08/17
******************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <iconv.h>
#include <errno.h>
#include "SDKpub.h"
#ifndef ERRFILE
#define ERRFILE "errlog"
#endif
char * EncodingConv( const char * in, char *encFrom, char *encTo )
{
char *buff, *sin, *sout;
int lenin, lenout;
iconv_t ct;
if( (ct=iconv_open(encTo, encFrom)) == (iconv_t)-1 )
{
SDKerrlog( ERRFILE, "%s|%d| iconv_open error! %s", __FILE__,
__LINE__, strerror(errno) );
return( NULL );
}
iconv( ct, NULL, NULL, NULL, NULL );
sin = (char *)in;
lenin = strlen(in) + 1;
if( (buff=malloc( lenin*2 ))==NULL )
{
SDKerrlog( ERRFILE, "%s|%d| malloc error! %s", __FILE__, __LINE__,
strerror(errno) );
iconv_close( ct );
return( NULL );
}
sout = buff;
lenout = lenin*2;
if( iconv( ct, &sin, (size_t *)&lenin, &sout, (size_t *)&lenout) == -1 )
{
SDKerrlog( ERRFILE, "%s|%d| iconv() error! errno=%d %s", __FILE__,
__LINE__, errno, strerror(errno) );
free( buff );
iconv_close( ct );
return NULL;
}
iconv_close( ct );
sout=strdup(buff);
free( buff );
return( sout );
}
|