当前位置: 技术问答>linux和unix
GB2312到unicode编码转换不成功???
来源: 互联网 发布时间:2017-03-05
本文导语: 最近在做mini2440控制gsm发送短信,在对信息进行GB2312到unicode编码转换时出现了乱码? 我用的是ubuntu 12.04 下面是我的代码 #include 2 #include 3 #include 4 #include //convert function 5 #include 6 #inclu...
最近在做mini2440控制gsm发送短信,在对信息进行GB2312到unicode编码转换时出现了乱码?
我用的是ubuntu 12.04
下面是我的代码
#include
2 #include
3 #include
4 #include //convert function
5 #include
6 #include
7 #include
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("%sn",to);
24 iconv_close(cd);
25 return ;
26 }
27
28 int main()
29 {
30 char from[]="你好";
31 char to[S]={''};
32 convert("gb2312","unicode",from,strlen(from),to,S);
33 printf("%sn",to);
34 return 0;
35 }
我的输出结果为:
你好 is to be converted!
Convert fail!
��cm
哪位大神给指点指点!!!不胜感激!!!
我用的是ubuntu 12.04
下面是我的代码
#include
2 #include
3 #include
4 #include //convert function
5 #include
6 #include
7 #include
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("%sn",to);
24 iconv_close(cd);
25 return ;
26 }
27
28 int main()
29 {
30 char from[]="你好";
31 char to[S]={''};
32 convert("gb2312","unicode",from,strlen(from),to,S);
33 printf("%sn",to);
34 return 0;
35 }
我的输出结果为:
你好 is to be converted!
Convert fail!
��cm
哪位大神给指点指点!!!不胜感激!!!
|
以前写的函数
#include
#include
#include
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;
}
|
|
检查vi中默认的中文是否为GB2312,如果不是手动设置成GB2312,然后再写你好