从 JNI 调用返回阿拉伯语
我一直在尝试从 JNI 调用返回 ARABIC 字符串。
java方法如下
private native String ataTrans_CheckWord(String lpszWord, String lpszDest, int m_flag, int lpszReserved);
lpszWord : 输入英文
lpszDest : 忽略
m_flag : 忽略
lpszReserved :Ignore
现在,当我使用 javah 生成头文件时,我得到一个带有此签名的 C++ 头文件
JNIEXPORT jstring JNICALL Java_MyClass_ataTrans_1CheckWord (JNIEnv* env, jobject, jstring, jstring, jint , jint)
现在,在这个 C++ 代码中,我有这样的语句,
JNIEXPORT jstring JNICALL Java_MyClass_ataTrans_1CheckWord(JNIEnv* env, jobject, jstring jstrInput, jstring, jint , jint)
{
char aa[10];
char* bb;
char** cc;
bb = aa;
cc = &bb;
jstring tempValue;
const char* strCIn = (env)->GetStringUTFChars(jstrInput , &blnIsCopy);
int retVal = pDllataTrans_CheckWord(strCIn, cc, m_flag, lpszReserved);
printf("Orginal Arabic Conversion Index 0: %s \n",cc[0]); //This prints ARABIC properly
tempValue = (env)->NewString((jchar* )cc[0],10); // convert char array to jstring
printf("JSTRING UNICODE Created : %s \n",tempValue); //This prints junk
return tempValue;
}
我相信 ARABIC 内容位于指向指针“cc”的指针内。最后,在我的java代码中,我有一个这样的调用,
String temp = myclassInstance.ataTrans_CheckWord("ABCDEFG", "",1, 0);
System.out.println("FROM JAVE OUTPUT : "+temp); //Prints Junk
我只是无法将一些阿拉伯字符返回到我的JAVA代码中。我做错了什么吗?我尝试了各种其他替代方案,例如
tempValue = env->NewStringUTF("شسيشسيشسيشس");
返回 tempValue 但没有运气。在JAVA端总是垃圾。
I have been trying to return an ARABIC string from a JNI call.
The java method is as follows
private native String ataTrans_CheckWord(String lpszWord, String lpszDest, int m_flag, int lpszReserved);
lpszWord : Input English
lpszDest : Ignore
m_flag : Ignore
lpszReserved :Ignore
Now when I use javah to generate the header file I get a C++ header file with this signature
JNIEXPORT jstring JNICALL Java_MyClass_ataTrans_1CheckWord (JNIEnv* env, jobject, jstring, jstring, jint , jint)
Now in this C++ code I have statements such as this
JNIEXPORT jstring JNICALL Java_MyClass_ataTrans_1CheckWord(JNIEnv* env, jobject, jstring jstrInput, jstring, jint , jint)
{
char aa[10];
char* bb;
char** cc;
bb = aa;
cc = &bb;
jstring tempValue;
const char* strCIn = (env)->GetStringUTFChars(jstrInput , &blnIsCopy);
int retVal = pDllataTrans_CheckWord(strCIn, cc, m_flag, lpszReserved);
printf("Orginal Arabic Conversion Index 0: %s \n",cc[0]); //This prints ARABIC properly
tempValue = (env)->NewString((jchar* )cc[0],10); // convert char array to jstring
printf("JSTRING UNICODE Created : %s \n",tempValue); //This prints junk
return tempValue;
}
I believe the ARABIC content is inside the pointer to a pointer “cc”. Finally in my java code I have a call like this
String temp = myclassInstance.ataTrans_CheckWord("ABCDEFG", "",1, 0);
System.out.println("FROM JAVE OUTPUT : "+temp); //Prints Junk
I just can’t get to return some ARABIC character out into my JAVA code. Is there something wrong I am doing? I have tried out various other alternates such as
tempValue = env->NewStringUTF("شسيشسيشسيشس");
and return tempValue but no luck. Its always garbage on the JAVA side.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Java 字符串内部采用
UTF-16
,这是一种每个字符使用 2 或 4 个字节的编码。您的翻译系统似乎返回以 MBCS(多字节字符集)编码的字符串 - 每个字符 1-N 字节。JNI
NewString
函数需要将数据编码为UTF-16
,而您向它传递的是其他内容 - 因此在 java 中您会得到垃圾数据。您的信息中缺少的一件事是您的翻译系统使用哪种编码。我假设它是UTF-8
,并使用MultiByteToWideChar
转换为 java 期望的格式。下面的代码假设您在 Windows 上执行此操作 - 如果不是,请指定平台,并查看例如iconv
库。如果您获取的字符串是其他代码页,请替换上面的 CP_UTF8。
顺便说一句,如果编码实际上是 UTF-8,您只需将
cc[0]
传递给NewStringUTF
- 此函数在内部处理 UTF-8 到 UTF-16 的转换。Java strings are internally
UTF-16
, an encoding which uses 2 or 4 bytes per character. Your translation system seems to return strings encoded in aMBCS
(Multi-Byte Character Set) - 1-N bytes per character.The JNI
NewString
function expects data encoded asUTF-16
, and you're passing it something else - so in java you get garbage data. The one thing that is lacking from your information is which encoding your translation system uses. I'll assume it'sUTF-8
, and useMultiByteToWideChar
to convert to the format java expects. The below code assumes that you're doing this on Windows - if not, specify platform, and look at e.g. theiconv
library.If you get strings as some other codepage, replace CP_UTF8 above.
As a side note, if the encoding actually is UTF-8, you can simply pass your
cc[0]
toNewStringUTF
instead - This function handles the UTF-8 to UTF-16 conversion internally.