Sunday, 18 August 2013

android jni string manipulating

android jni string manipulating

I want to manipulate string in JNI side of code but when I get to
manipulate always return a wrong result, if I click on button to get
result always result is difference I test this code in Java and C# word
well.
My JNI code is :
JNIEXPORT jstring JNICALL Java_com_test_ndk_MainActivity_strTest(JNIEnv*
env, jobject thiz, jstring enc) {
const utf8 *nativeString = (*env)->GetStringUTFChars(env, enc, 0);
utf8 *newstr;
utf8 *left;
utf8 *right;
int encLenght = strlen(nativeString);
int middl = encLenght / 2;
int i;
newstr = subString(nativeString, 0, middl);
int lenght = strlen(newstr);
left = malloc(lenght);
for (i = 0; i < lenght; i++) {
left[i] = newstr[i] + 1;
}
newstr = subString(nativeString, middl, encLenght - middl);
lenght = strlen(newstr);
right = malloc(lenght);
for (i = 0; i < lenght; i++) {
right[i] = newstr[i] -1;
}
strcat(right, left);
(*env)->ReleaseStringUTFChars(env, enc, nativeString);
return (*env)->NewStringUTF(env, right);
}
This give a string and simple encrypt, I think my problem in memory control!
And this is my substring function:
utf8* subString(const utf8 *str, int begin, int len) {
return strndup(str + begin, len);
}

No comments:

Post a Comment