如何在JNI中将int转换为String(?)?
我有一个 int [] 数组,我想将其每个元素转换为 String >(?)在 JNI 中,最后将它们连接为 String (?)(包括逗号)
I had got a int[ ] array, and I want to convert each element of it into String(?) in JNI, and finally concatenate them as a String(?) (include the comma).
例如:
// java code
int testIntArray = new int[]{1, 2, 3};
String arrayString = "";
jni.constructArrayString(testIntArray, arrayString);
// the print content should like this: 1,2,3
System.out.println("ArrayString: " + arrayString);
// jni code
JNIEXPORT void JNICALL constructArrayString (JNIEnv *env, jobject obj, jintArray jArr, jstring jstr) {
// to do sth.
// code maybe like the follow
jint *arr = env -> GetIntArrayElements(jArr, 0);
int len = env -> GetArrayLength(jArr);
char *c_str = env -> GetStringUTFChars(jstr, 0);
if(c_str == NULL) {
return;
}
for(int i = 0; i < len; i++){
// how to concatenate the arr[i], arr[i+1] and the comma ','
// and finally make the arrayString like the string: 1,2,3 ?
}
}
我知道,没有直接的方法可以将 int-type 转换为 string-type 数据或其他内容,但应该可以在 JNI 中使用,最后将它们连接为 String !
I know that, there's no a direct way which just can convert the int-type into string-type data or something else but should be operable in JNI, and concatenate them as a String at last!
如果很难使用 void 返回类型,只需进行更改即可!谢谢,谢谢!
If it is hard to do with the void return-type, just change it! Thanx, in advance!
================================================ =========================== 新问题:
=========================================================================== New problem:
首先,非常感谢@Jorn Vernee的回答,这似乎是我应该采取的好方法.但是,当我尝试这种方式时,会出现关于 std :: stringstream 的棘手问题.好吧,即使实例化它也会使应用程序崩溃.而且,可悲的是,我是 JNI 的新手,没有调试JVM运行时错误导致的崩溃问题的经验.我检查了@Moe Bataineh 问题,它确实看起来像我,但是它在Windows上与 MiniGW 或 Cygwin 一起使用,这是我不知道的,因此对我无能为力.
First, thanks @Jorn Vernee answer so much, it's seems the good way I should take. However, when I'm trying this way, there's comes an intractable problem about the std::stringstream. Well, even instantiating it will crash the application. And, sadly I'm new to JNI, have no experience in debugging a crash problem with JVM runtime error. And I had checked @Moe Bataineh question which really seems like me, however it's applying on Windows with MiniGW or Cygwin something I don't know, so it's hlepless for me.
JNI中的代码如下:
Code in JNI like this:
#include "utils_JniInterface.h"
#include <android/log.h>
#include <string.h>
#include <iostream>
#include <sstream>
using namespace std;
#define TAG "JNI-Log"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)
JNIEXPORT jstring JNICALL Java_utils_JniInterface_constructRGBArrayString (JNIEnv *env, jobject obj, jintArray jArr){
jint *arr = env -> GetIntArrayElements(jArr, 0);
int len = env -> GetArrayLength(jArr);
std::stringstream result;
for(int i = 0; i < len; i++) {
result << arr[i];
if(i < len - 1) {
result << ',';
}
}
env -> ReleaseIntArrayElements(jArr, arr, 0);
return env -> NewStringUTF(result.str().data());
}
// int[] a = {1,2,3} ⇒ String b = "1,2,3"
有关此问题的任何好的想法或建议吗?
Any good ideas or suggestions about this problem?
helpful link: Issue regarding iostream in android NDK
When utilizing std::stringstream ,I also got the following error;
严重错误:找不到"sstream"文件
fatal error: 'sstream' file not found
#include <sstream>
^产生了1个错误.
^ 1 error generated.
对我有帮助的解决方案是创建一个名为" Application.mk "的文件(注意:区分大小写).您需要添加的唯一一行是:
Solution that helped me was creating a file called "Application.mk" (Note: case-sensitive). The only line you need to add is:
APP_STL:= stlport_static
APP_STL := stlport_static
将" Application.mk "文件放入"jni"文件夹中,该文件夹与"Android.mk"文件所在的位置相同.这在eclipse中对我有用,我可以假设它也对android studio也有效.
以下是其他链接: Android ndk-build iostream:没有这样的文件或目录
希望这对@frank jorsn有帮助
Put the "Application.mk" file in your "jni" folder this is same location your "Android.mk" file is. This worked for me in eclipse, and I can assume it works for android studio too.
Here's a link with alternatives: Android ndk-build iostream: No such file or directory
Hope this helps @frank jorsn