0

I have an issue using the Android NDK to encode a simple CharArray using XOR and it keeps crashing. What's wrong here? Any hints?

Java:

public native char[] encrypt(char[] test);
encrypt("test".toCharArray()); 

C++:

#include <jni.h>
extern "C" JNIEXPORT jcharArray JNICALL

Java_com_example_MainActivity_encrypt(
        JNIEnv* env,
        jobject obj,
        jcharArray value
) {
    jboolean t = JNI_FALSE;
    jchar *carr;
    carr = env->GetCharArrayElements(value,&t);

    for (int i=0; i< (env->GetArrayLength((jcharArray)carr)); i++) {
        carr[i] = carr[i] ^ 'N';
    }

    return (jcharArray)carr;
}
Botje
  • 26,269
  • 3
  • 31
  • 41
Nils
  • 1,705
  • 6
  • 23
  • 32
  • check this out https://samplecodebank.blogspot.com/2013/04/jni-jnienv-getchararrayelements-example.html. look at the sample example given at the bottom section – Harry Nov 16 '20 at 11:03
  • You can't cast a C array pointer to a Java array type. You'll have to create a new `jcharArray` using the `NewCharArray` method available in your `JNIEnv`. – Michael Nov 16 '20 at 11:09
  • The question I linked to is for an `int[]`, but the principle is the same for a `char[]`. – Michael Nov 16 '20 at 11:14
  • Take a look here: https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo014 – Oo.oO Nov 16 '20 at 19:41

0 Answers0