当前位置: 技术问答>linux和unix
[高分求救啊]jni的C++实现中,调用iconv转UTF-8到GB18030老失败
来源: 互联网 发布时间:2016-02-25
本文导语: 在linux下转UTF-8到GB18030,结果老为空,都试了几天了,结果都不正确,谁能救救我啊,以下是我的源代码: EncodeMessage:该参数类型为jsring,EncodeMessage="ab你好吗"(从java传过来的) char *temp;//=""; int len,reqBuffLen; size_t...
在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-8,即Unicode
//reqBuffLen=env->GetStringLength(EncodeMessage);
temp=(char*)env->GetStringUTFChars(EncodeMessage,0);//返回的应当是UTF-8
reqBuffLen=env->GetStringUTFLength(EncodeMessage);
length = reqBuffLen;
printf("temp=%sn",temp);//正确显示
printf("reqBuffLen=%dn",reqBuffLen);
char *pmbbuf=(char*)malloc(length);
iconv_t cd;
// cd=iconv_open("GB18030","utf-8");
cd=iconv_open("GB18030","UTF-8");//成功
if (cd==0){
printf("iconv_open failsn");
return -1;
}
size_t rt;
if(rt=(int)iconv(cd,&temp,&length,&pmbbuf,&length2)==-1){//成功
printf("iconv failsen");
return -1;
}
iconv_close(cd);
printf("pmbbuf=%sn",pmbbuf);//pmbbuf结果不是空,就是乱七八糟的东西
printf("len=%dn",length2);
结果不兑,那位大侠给看看啊,急死我了
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-8,即Unicode
//reqBuffLen=env->GetStringLength(EncodeMessage);
temp=(char*)env->GetStringUTFChars(EncodeMessage,0);//返回的应当是UTF-8
reqBuffLen=env->GetStringUTFLength(EncodeMessage);
length = reqBuffLen;
printf("temp=%sn",temp);//正确显示
printf("reqBuffLen=%dn",reqBuffLen);
char *pmbbuf=(char*)malloc(length);
iconv_t cd;
// cd=iconv_open("GB18030","utf-8");
cd=iconv_open("GB18030","UTF-8");//成功
if (cd==0){
printf("iconv_open failsn");
return -1;
}
size_t rt;
if(rt=(int)iconv(cd,&temp,&length,&pmbbuf,&length2)==-1){//成功
printf("iconv failsen");
return -1;
}
iconv_close(cd);
printf("pmbbuf=%sn",pmbbuf);//pmbbuf结果不是空,就是乱七八糟的东西
printf("len=%dn",length2);
结果不兑,那位大侠给看看啊,急死我了
|
try to set konsole encoding .
go home , byebye, ha ha
go home , byebye, ha ha