C ++ std :: string到jstring,长度固定 [英] C++ std::string to jstring with a fixed length
问题描述
我正在尝试将C ++ std :: string转换为jstring并返回它。这很容易
I'm trying to turn a C++ std::string into a jstring and return it. This would be easy enough with
JNIEnv*->NewStringUTF(stdString.c_str())
但问题是我正在转换的字符串几乎随机散布了空字符。这是 c_str()
的问题,但不是 std :: string
。 NewStringUTF
只会捕获完整 std :: string
的一部分。有一些希望, std :: string
有一个 length()
函数,它获取全长,忽略有问题的char * \0
字符。
but the problem is that the string I'm converting has almost randomly interspersed null characters in it. This is a problem for the c_str()
, but not the std::string
. NewStringUTF
will only catch a portion of the full std::string
. There is some hope in that the std::string
has a length()
function, which gets the full length, ignoring the problematic char* \0
characters.
有一个单独的函数NewString ,它接受一个jchar *和一个jsize *,所以看起来很有希望,但是我无法将std :: string正确转换为jchar *。我试过把它变成一个字节数组,但我可能做得不对。我有进一步的问题,将 length()
给出的int转换为jsize,这是NewString调用所需要的。
There's a separate function NewString that takes in a jchar* and a jsize*, so that looks promising, but I can't get the std::string converted properly to the jchar*. I tried making it a byte array, but I probably wasn't doing it right. I had further problems converting the int given by the length()
into a jsize, which was required by the NewString call.
我已经用 vector< char>做了一些工作。 byteArray(stdString.begin(),stdString.end())
,但这并没有让我走得太远,可能是因为这弄乱了原始字符串是什么。
I have done a bit of work with vector<char> byteArray(stdString.begin(), stdString.end())
, but that didn't get me very far, probably because that is messing up what the original string is.
这是我的基本启动函数,它使用没有空字符的字符串:
Here is the basic starter function I have, which worked with strings without null chars:
jstring StringToJString(JNIEnv * env, const std::string & nativeString) {
return env->NewStringUTF(nativeString.c_str());
}
作为旁注,此函数正在JNI包装器文件中使用返回一个对象的 std :: string
。
As a side note, this function is being used inside a JNI wrapper file for returning an object's std::string
.
感谢您提供任何帮助或信息来源!
Thanks for any help or information sources!
推荐答案
我根据RedAlert的建议制定了自己的解决方法。
I made my own workaround solution taking RedAlert's advice.
Java现在需要一个字节[来自本机电话:
Java now expects a byte[] from the native call:
private native byte[] toString(long thiz);
toString方法现在在 std :: string中调用此方法
:
The toString method now calls this method inside it on a std::string
:
jbyteArray StringToJByteArray(JNIEnv * env, const std::string &nativeString) {
jbyteArray arr = env->NewByteArray(nativeString.length());
env->SetByteArrayRegion(arr,0,nativeString.length(),(jbyte*)nativeString.c_str());
return arr;
}
并且java级别接收如下数据:
And the java level receives the data like this:
byte[] byteArray = toString(mNativeInstance);
String nativeString = "";
try {
nativeString = new String(byteArray, "UTF-8");
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "Couldn't convert the jbyteArray to UTF-8");
e.printStackTrace();
}
这篇关于C ++ std :: string到jstring,长度固定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!