[高分求救啊]jni的C++实现中,调用iconv转UTF-8到GB18030老失败

tannafe 2007-03-01 04:02:25
在linux下转UTF-8到GB18030,结果老为空,都试了几天了,结果都不正确,谁能救救我啊,以下是我的源代码:
EncodeMessage:该参数类型为jsring,EncodeMessage="ab你好吗"(从java传过来的)
char *temp;//="";
int len,reqBuffLen;
size_t length,length2=100,length3=100;
const jchar *jwstr;

//temp=(char*)env->GetStringChars(EncodeMessage,0);//返回的应当是UTF-16,即Unicode
//reqBuffLen=env->GetStringLength(EncodeMessage);
temp=(char*)env->GetStringUTFChars(EncodeMessage,0);//返回的应当是UTF-8
reqBuffLen=env->GetStringUTFLength(EncodeMessage);
length = reqBuffLen;

printf("temp=%s\n",temp);//正确显示
printf("reqBuffLen=%d\n",reqBuffLen);
char *pmbbuf=(char*)malloc(length);
iconv_t cd;
// cd=iconv_open("GB18030","UTF-16");
cd=iconv_open("GB18030","UTF-8");//成功
if (cd==0){
printf("iconv_open fails\n");
return -1;
}

size_t rt;
if(rt=(int)iconv(cd,&temp,&length,&pmbbuf,&length2)==-1){//成功
printf("iconv failse\n");
return -1;
}
iconv_close(cd);
printf("pmbbuf=%s\n",pmbbuf);//pmbbuf结果不是空,就是乱七八糟的东西
printf("len=%d\n",length2);

结果不兑,那位大侠给看看啊,急死我了
...全文
837 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
tangyulong1214 2008-06-27
  • 打赏
  • 举报
回复
我也谢谢二位!
tannafe 2007-03-02
  • 打赏
  • 举报
回复
今天结帖给分,谢谢:bobya2003() 大侠,以后不吝赐教哦
bobya2003 2007-03-01
  • 打赏
  • 举报
回复
try to set konsole encoding .

go home , byebye, ha ha
tannafe 2007-03-01
  • 打赏
  • 举报
回复
bobya2003() 大侠,俺问题解决了,我写了另外一个程序测试通过。
关键的是
char *pmbbuf=(char*)malloc(length);
char *pmbbuf1= pmbbuf; //this line 好关键哦
以后不吝赐教啊

还有个问题
printf( "pmbbuf1=%s\n ",pmbbuf1); //汉字打印不出来,不知咋回事
bobya2003 2007-03-01
  • 打赏
  • 举报
回复
and another line
char *temp = new char[20];
bobya2003 2007-03-01
  • 打赏
  • 举报
回复
size_t length2=220;
char *pmbbuf= new char [100];

only these are different from you, my result is OK.
bobya2003 2007-03-01
  • 打赏
  • 举报
回复
printf("pmbbuf1=%s\n",pmbbuf1);

here
printf("pmbbuf=%s\n",pmbbuf);
iconv result is pmbbuf, the pmbbuf1 is not point at the pmbbuf[0]
tannafe 2007-03-01
  • 打赏
  • 举报
回复
实际上我已经改为了
char *pmbbuf=(char*)malloc(100);//在iconv处还是出同样的错误啊
bobya2003 2007-03-01
  • 打赏
  • 举报
回复
memset(pmbbuf,0x00,100)
this line , not 100 ,is length+10
tannafe 2007-03-01
  • 打赏
  • 举报
回复
bobya2003()大哥,还是不行啊,更该后的代码如下:

char temp[20];
int length2=220;//够长了吧
strcpy(temp,"ab你好吗");
printf("temp=%s\n",temp);//
size_t length = strlen(temp);
//printf("reqBuffLen=%d\n",reqBuffLen);
char *pmbbuf=(char*)malloc(length+10);//pmbbuf内存空间够大了吧
char *pmbbuf1= pmbbuf;
memset(pmbbuf,0x00,100);
iconv_t cd;
// cd=iconv_open("GB18030","UTF-16");
cd=iconv_open("GBK","utf8");//成功
if (cd==0){
printf("iconv_open fails\n");
exit(-1);
}

size_t rt;
printf("iconv start\n");
if(rt=(int)iconv(cd,&temp,&length,&pmbbuf1,&length2)==-1){//报错为“段错误”
perror("iconv failse\n");
// return -1;
}
printf("iconv end\n");
iconv_close(cd);
printf("pmbbuf1=%s\n",pmbbuf1);
printf("len=%d\n",length);
printf("len=%d\n",length2);

在if(rt=(int)iconv(cd,&temp,&length,&pmbbuf1,&length2)==-1)处报错啊,不知怎么回事
以上程序在RedHat Linux as 4 测试
bobya2003 2007-03-01
  • 打赏
  • 举报
回复
i test OK under gentoo as follows.
///////////////////////////////////////////
strcpy(temp,"ab你好吗");
printf("temp=%s\n",temp);//?ȷ?ʾ
length = strlen(temp);
printf("reqBuffLen=%d\n",reqBuffLen);
char *pmbbuf=(char*)malloc(length);
char *pmbbuf1= pmbbuf;
memset(pmbbuf,0x00,100);
iconv_t cd;
// cd=iconv_open("GB18030","UTF-16");
cd=iconv_open("GBK","utf8");//ɹ
if (cd==0){
printf("iconv_open fails\n");
exit(-1);
}

size_t rt;
if(rt=(int)iconv(cd,&temp,&length,&pmbbuf1,&length2)==-1){//ɹ
perror("iconv failse\n");
// return -1;
}
iconv_close(cd);
printf("len=%d\n",length);
printf("len=%d\n",length2);
/////////////////////////////////
char *pmbbuf=(char*)malloc(length);
char *pmbbuf1= pmbbuf;

here,you need to define another pointer ,because iconv will change it.
by the way,you do not free the memory.

bobya2003 2007-03-01
  • 打赏
  • 举报
回复
char *pmbbuf=(char*)malloc(length);
length maybe short , try to new a long buffer.

then printf length and length2 begin iconv and after iconv to find the problem
bobya2003 2007-03-01
  • 打赏
  • 举报
回复

hi.baidu.com/makeff

23,125

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧