Android JNI C simple append function

[亡魂溺海] 提交于 2020-06-25 05:30:01

问题


Id like to make a simple function, that return value of two Strings.

basically:

java

public native String getAppendedString(String name);

c

jstring Java_com_example_hellojni_HelloJni_getAppendedString(JNIEnv* env,   jobject thiz, jstring s) {
    jstring sx = (*env)->GetStringUTFChars(env, s, NULL);

    return ((*env)->NewStringUTF(env, "asd ")+sx);
}

its say

jni/hello-jni.c:32: warning: initialization discards qualifiers from pointer target type jni/hello-jni.c:34: error: invalid operands to binary + (have 'char *' and 'char *')

the retval will be: "asd qwer", how can I do this?

edit

jstring s1  = (*env)->NewStringUTF(env, "456");
jstring s2  = (*env)->NewStringUTF(env, "123");
jstring sall=strcat(s1, s2);

return sall;

only return "456"

edited #3 THE LAST

the final working code is:

jstring Java_com_example_hellojni_HelloJni_getAppendedString(JNIEnv* env,   jobject thiz, jstring s1) {
    D("HMMMMMMMMMMMMMMMMMMM");
    jstring s2  = (*env)->NewStringUTF(env, "123");

    jbyte *s1x  = (*env)->GetStringUTFChars(env, s1, NULL);
    jbyte *s2x  = (*env)->GetStringUTFChars(env, s2, NULL);

    char *sall  = malloc(strlen(s1x) + strlen(s2x) + 1);
    strcpy(sall, s1x);
    strcat(sall, s2x);

    jstring retval = (*env)->NewStringUTF(env, sall);

    (*env)->ReleaseStringUTFChars(env, s1, s1x);
    (*env)->ReleaseStringUTFChars(env, s2, s2x);
    free(sall);

    return retval;
}

Thanks, Leslie


回答1:


There are a few issues here:

  1. GetStringUTFChars returns a jbyte * (a null-terminated C string), not a jstring. You need this C string to do string manipulation in C.

  2. You need to call ReleaseStringUTFChars when you're done with it.

  3. You need to allocate enough memory to hold the concatenated string, using malloc.

  4. As ethan mentioned, you need to concatenate your two C strings with strcat. (You cannot do this with the + operator. When applied to a pointer, + returns the pointer from the offset of the original pointer.)

  5. Remember to free the memory you allocated after you're done with it (ie, after it's been interned as a Java string.)

You should do something along the lines of:

char *concatenated;
const jbyte *sx;
jstring retval;

/* Get the UTF-8 characters that represent our java string */
sx = (*env)->GetStringUTFChars(env, s, NULL);

/* Concatenate the two strings. */
concatenated = malloc(strlen("asd ") + strlen(sx) + 1);
strcpy(concatenated, "asd ");
strcat(concatenated, sx);

/* Create java string from our concatenated C string */
retval = (*env)->NewStringUTF(env, concatenated);

/* Free the memory in sx */
(*env)->ReleaseStringUTFChars(env, s, sx);

/* Free the memory in concatenated */
free(concatenated);

return retval;



回答2:


You can't concatenate two char* with + in c++. Try using strcat instead.

http://www.cplusplus.com/reference/clibrary/cstring/strcat/

EDIT:

from the documentation for strcat:

char * strcat ( char * destination, const char * source );

Concatenate strings

Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a new null-character is appended at the end of the new string formed by the concatenation of both in destination.

This means that the first argument to strcat needs to have enough memory allocated to fit the entire concatenated string.



来源:https://stackoverflow.com/questions/8301206/android-jni-c-simple-append-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!