形参不用size_t 会出错
#include <iconv.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#define RET_OK 0
#define RET_ERR -1
#define PRINTF_DEBUG printf
int api_iconv(const char *in_code, char *in_buf, size_t in_len,const char *out_code, char *out_buf, size_t out_buf_max, size_t *out_len)
{
int ret;
iconv_t cd;
char *p_in, *p_out;
p_in = in_buf;
p_out = out_buf;
*out_len = out_buf_max;
cd = iconv_open(out_code, in_code);
if ((iconv_t)(-1) == cd){
PRINTF_DEBUG("iconv_open error!. errno %d in %s %s.\n", errno, in_code, out_code);
return RET_ERR;
}
ret = iconv(cd, &in_buf, &in_len, &out_buf, out_len);
*out_len = out_buf_max - *out_len;
iconv_close(cd);
if (0 != ret){
PRINTF_DEBUG("iconv error!. %d errno %d in %2s out %2s %zu %zu %s %s\n", ret, errno, p_in, p_out, in_len, *out_len, in_code, out_code);
return RET_ERR;
}
PRINTF_DEBUG("in %s, out %s %zu.\n", p_in, p_out, *out_len);
return RET_OK;
}
int main()
{int ret = -1;
int len, len2, cc;
char utf8[100] = "abc您好中国";
char buff[102] ={ 0 };
len2 = strlen(utf8);
cc = sizeof(buff);
ret = api_iconv("UTF-8",utf8,len2,"GB2312",buff, cc,&len);
printf("%s %d \n", utf8, len);
printf("%s %d\n", buff, strlen(buff));
return 0;
}
gcc -o t4 in4.c -liconv
[root@localhost test]# ./t4
SRC , DST 11.
abc您好中国 11
abc????й? 11