GB2312到unicode编码转换不成功???
最近在做mini2440控制gsm发送短信,在对信息进行GB2312到unicode编码转换时出现了乱码?
我用的是ubuntu 12.04
下面是我的代码
#include <unistd.h>
2 #include <stdio.h>
3 #include <sys/types.h>
4 #include <iconv.h> //convert function
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include<string.h>
8
9 #define S 2000
10
11 void convert(const char *fromset,const char *toset,char *from,int from_len,char *to,int to_len)
12 {
13 printf("%s is to be converted!\n",from);
14 iconv_t cd,cdd;
15 cd=iconv_open(toset,fromset);
16 char **from2=&from;
17 char **to2=&to;
18 if(iconv(cd,from2,&from_len,to2,&to_len)==-1)
19 printf("Convert fail!\n");
20 else
21 printf("Convert success!\n");
22
23 // printf("%s\n",to);
24 iconv_close(cd);
25 return ;
26 }
27
28 int main()
29 {
30 char from[]="你好";
31 char to[S]={'\0'};
32 convert("gb2312","unicode",from,strlen(from),to,S);
33 printf("%s\n",to);
34 return 0;
35 }
我的输出结果为:
你好 is to be converted!
Convert fail!
??cm
哪位大神给指点指点!!!不胜感激!!!
------解决方案--------------------可以参考一下我之前搞的这个http://blog.csdn.net/brantyou/article/details/7306029
------解决方案--------------------检查vi中默认的中文是否为GB2312,如果不是手动设置成GB2312,然后再写你好
------解决方案--------------------
以前写的函数
C/C++ code
#include <stdio.h>
#include <string.h>
#include <iconv.h>
char *ks_iconv(const char *str, char *buf, const char *from, const char *to)
{
iconv_t cd;
char *inbuf;
char *outbuf;
char *dstbuf;
size_t dstlen;
size_t size;
size_t inleft;
size_t outleft;
dstlen = strlen(str) * 3;
dstbuf = buf;
cd = iconv_open(to, from);
if ((iconv_t)-1 == cd) {
return 0;
}
inbuf = (char *)str;
outbuf = dstbuf;
inleft = strlen(str);
outleft = dstlen;
size = iconv(cd, &inbuf, &inleft, &outbuf, &outleft);
iconv_close(cd);
if ((size_t)-1 == size) {
return NULL;
}
if (inleft > 0) {
return NULL;
}
dstbuf[dstlen - outleft] = 0;
return (dstbuf);
}
int main(int argc, char *argv[])
{
char gb[] = "中国";
char utf[32];
ks_iconv(gb, utf, "GB2312", "UTF-8");
printf("utf=[%s]\n", utf);
return 0;
}