64位linux
-
#include <stdio.h>
-
#include <iconv.h>
-
#include <string.h>
-
-
int code_conv(const char* fromcode,const char* tocode,const char* str_in,int inlen,char* str_out,int outsize)
-
{
-
if (NULL==str_in || inlen<=0 || NULL==str_out || outsize<=0)
-
{
-
return -1; //入参错误
-
}
-
-
iconv_t i_cv = iconv_open(tocode, fromcode);
-
printf(" i_cv :%ld\n", i_cv);
-
if ((iconv_t)-1 == i_cv)
-
{
-
return -2; //转换描述符申请失败
-
}
-
-
printf("sizeof(iconv_t):%d\n", sizeof(iconv_t));
-
char** pp_in = (char**)&str_in;
-
char** pp_out = (char**)&str_out;
-
int b=1;
-
int inleft = inlen;
-
int a=1;
-
size_t outleft = outsize;
-
-
if ((size_t)-1 == iconv(i_cv, pp_in, (size_t*)&inleft, pp_out, (size_t*)&outleft))
-
{
-
perror("iconv");
-
printf(" i_cv :%ld\n", i_cv);
-
iconv_close(i_cv);
-
return -3; //转码失败
-
}
-
-
iconv_close(i_cv);
-
return outsize-outleft; //转码后的长度
-
}
-
-
main()
-
{
-
char *in_utf8 = "姝e?ㄥ??瑁?";
-
char *in_gb2312 = "正在安装";
-
char str_out[512] = {0}; //据chenx说,此处out数组小可能导致转码失败
-
int in_len = strlen(in_gb2312);
-
int out_size = sizeof(str_out);
-
-
int n = code_conv("gb2312", "utf-8", in_gb2312, in_len, str_out, out_size);
-
if(n > 0)
-
{
-
printf("out:%s\n", str_out);
-
printf("out len:%d\n", n);
-
}
-
else
-
{
-
perror("code_conv");
-
}
-
printf("out:%s\n", str_out);
-
printf("out len:%d\n", n);
-
}
-
i_cv :324074912
-
sizeof(iconv_t):8
-
iconv: Invalid or incomplete multibyte or wide character
-
i_cv :324074911
-
段错误 (core dumped)
(size_t*)&inleft的用法显然是不对的。i_cv的值减少了1!如果在iconv_close尝试给i_cv加1,则报错:code_conv.cpp:30: 错误:‘void *’ 型指针用在了算术表达式
这是怎么发生的呢?
(未完待续)
阅读(1143) | 评论(0) | 转发(0) |