- 論壇徽章:
- 0
|
下面是代碼,轉(zhuǎn)化成功了,但iconv返回-1,errno為84,errno.84 is: Invalid or incomplete multibyte or wide character
不知道原因,請(qǐng)高手指點(diǎn)。- #include <iconv.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <errno.h>
- #define OUTLEN 255
- //代碼轉(zhuǎn)換:從一種編碼轉(zhuǎn)為另一種編碼
- int code_convert(char *from_charset,char *to_charset,char *inbuf,int inlen,char *outbuf,int outlen)
- {
- iconv_t cd;
- int rc;
- char **pin = &inbuf;
- char **pout = &outbuf;
- cd = iconv_open(to_charset,from_charset);
- if (cd==0) return -1;
- memset(outbuf,0,outlen);
- if (iconv(cd,pin,(size_t*)&inlen,pout,(size_t*)&outlen)==-1) return -1;
- iconv_close(cd);
- return 0;
- }
- //UNICODE碼轉(zhuǎn)為GB2312碼
- int u2g(char *inbuf,int inlen,char *outbuf,int outlen)
- {
- return code_convert("utf-8","gb2312",inbuf,inlen,outbuf,outlen);
- }
- //GB2312碼轉(zhuǎn)為UNICODE碼
- int g2u(char *inbuf,size_t inlen,char *outbuf,size_t outlen)
- {
- return code_convert("gb2312","utf-8",inbuf,inlen,outbuf,outlen);
- }
- main()
- {
- char *in_utf8 = "姝e?ㄥ??瑁?";
- char *in_gb2312 = "正在安裝";
- char out[OUTLEN];
- //unicode碼轉(zhuǎn)為gb2312碼
- int rc = u2g(in_utf8,strlen(in_utf8),out,OUTLEN);
- printf("rc:%d, errno:%d, unicode-->gb2312 out=%sn",rc,errno, out);
- //gb2312碼轉(zhuǎn)為unicode碼
- rc = g2u(in_gb2312,strlen(in_gb2312),out,OUTLEN);
- printf("rc:%d, errno:%d, gb2312-->unicode out=%sn",rc,errno,out);
- }
復(fù)制代碼 |
|