从 JNI 调用返回阿拉伯语

发布于 2024-10-22 05:23:02 字数 1543 浏览 5 评论 0原文

我一直在尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

牛↙奶布丁 2024-10-29 05:23:02

Java 字符串内部采用 UTF-16,这是一种每个字符使用 2 或 4 个字节的编码。您的翻译系统似乎返回以 MBCS(多字节字符集)编码的字符串 - 每个字符 1-N 字节。

JNI NewString 函数需要将数据编码为 UTF-16,而您向它传递的是其他内容 - 因此在 java 中您会得到垃圾数据。您的信息中缺少的一件事是您的翻译系统使用哪种编码。我假设它是 UTF-8,并使用 MultiByteToWideChar 转换为 java 期望的格式。下面的代码假设您在 Windows 上执行此操作 - 如果不是,请指定平台,并查看例如 iconv 库。

int Len = strlen(cc[0])*2+2;
wchar_t * Buffer = (wchar_t *) malloc(Len);
MultiByteToWideChar(CP_UTF8, 0, cc[0], -1, Buffer, Len);
tempValue = (env)->NewString((jchar* )Buffer,wcslen(Buffer));
free(Buffer);

如果您获取的字符串是其他代码页,请替换上面的 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 a MBCS (Multi-Byte Character Set) - 1-N bytes per character.

The JNI NewString function expects data encoded as UTF-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's UTF-8, and use MultiByteToWideChar 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. the iconv library.

int Len = strlen(cc[0])*2+2;
wchar_t * Buffer = (wchar_t *) malloc(Len);
MultiByteToWideChar(CP_UTF8, 0, cc[0], -1, Buffer, Len);
tempValue = (env)->NewString((jchar* )Buffer,wcslen(Buffer));
free(Buffer);

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] to NewStringUTF instead - This function handles the UTF-8 to UTF-16 conversion internally.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文