日期:2014-05-16  浏览次数:20710 次

linux c/c++一个关于gb2312转gbk的问题
ret = iconv(ct, &inbuf, &inbuf_sz, &otbuf, &otbuf_sz);
转化前inbuf is “测试”,inbuf_sz = strlen(inbuf);inbuf_sz此时大小为 4;
转化后outbuf is “测”;
ret = -1;errno == EINVAL;inbuf_sz = 1;转化失败;
但是如果我把inbuf_sz = strlen(inbuf)+1,则转化成功;
我把inbuf改成“测试1”后,同样能转化成功;跪求大神解释,和处理方法



------解决方案--------------------
PHP code
  iconv_t iconv_open(const char *tocode, const char *fromcode); 
  size_ticonv(iconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft); 
  int iconv_close(iconv_t cd); 
  iconv_open函数用来打开一个编码转换的流,iconv函数的作用是实际进行转换,iconv_close函数的作用就是关闭这个流。实际用法参见下面的例子,下面是一个将UTF-8码转换成GBK码的例子,我们假设已经有了一个uft8编码的输入缓冲区inbuf以及这个缓冲区的长度inlen。 
  iconv_t cd = iconv_open( "GBK", "UTF-8"); 
  char *outbuf = (char *)malloc(inlen * 4 ); 
  bzero( outbiuf, inlen * 4); 
  char *in = inbuf; 
  char *out = outbuf; 
  size_t outlen = inlen *4; 
  iconv(cd, &in, (size_t *)&inlen, &out,&outlen); 
  outlen = strlen(outbuf); 
  printf("%s\n",outbuf); 
  free(outbuf); 
  iconv_close(cd); 
  非常值得注意的地方是:iconv函数会修改参数in和参数out指针所指向的地方,也就是说,在调用iconv函数之前,我们的in和inbuf指针以及out和outbuf指针指向的是同一块内存区域,但是调用之后out指针所指向的地方就不是outbuf了,同理in指针。所以要 
  char *in = inbuf; 
  char *out = outbuf; 
  另存一下,使用或者释放内存的时候也要使用原先的那个指针outbuf和inbuf。

------解决方案--------------------
C/C++ code
#include <stdio.h>
#include <iconv.h>

int main(void)
{
    char inbuf[] = "解决方法很简单";
    char outbuf[32];
    int ret;
    size_t inlen = strlen(inbuf);

    iconv_t cd = iconv_open("GBK", "UTF-8");
    char *in = inbuf;
    size_t inl = inlen;
    char *out = outbuf;
    size_t outl = 32;
    ret = iconv(cd, &in, &inl, &out, &outl);
    iconv_close(cd);
    if (ret < 0) {
        printf("iconv failed\n");
        return -1;
    }
    printf("iconv ok, outlen=%d\n", (int)outl);
    printf("%s\n", outbuf);
    
    return 0;
}

------解决方案--------------------
楼主估计是不懂编码, 你能确认你之前的编码就是gbk?
------解决方案--------------------
貌似各个标准的字符集:

gb18030 > gb2312 > gbk