33

I'm newbie in C++ and JNI, I try to find a correct way to convert byte[] in java to unsigned char* in C++ by using JNI, and vice versa ! (I'm working on android) After looking for a solution in google and SO, I haven't found a good details way to convert byte[] in java to C++. Please help me, and provide a solution for a vice versa (unsigned char* in C++ to byte[] in java). Thanks very much

  • byte[] in java to unsigned char* in C++:

JAVA :

private static native void nativeReceiveDataFromServer(byte[] value, int length);

JNI:

... (JNIEnv* env, jobject thiz, jbyteArray array, jint array_length)
{
    ???
}

PS: I modified my question for being a real question for my problem :(

Hien Nguyen
  • 744
  • 2
  • 7
  • 20
  • Well of course it is. You can't write C++ in C. Not a real question. – user207421 May 21 '13 at 09:56
  • @EJP I think you hasn't read all my question ! The main question is "I'm newbie in C++ and JNI, I try to find a correct way to convert byte[] in java to unsigned char* in C++ by using JNI, and vice versa !.... Please correct me if I'm wrong, and provide a solution for a vice versa (unsigned char* in C++ to byte[] in java)" ! May you help me ? I think you shouldn't downgrade my point if you hasn't read all the question! – Hien Nguyen May 21 '13 at 10:00
  • seems to me like you're trying to use a c compiler to compile c++ code. – Zharf May 21 '13 at 10:01
  • I searched some post in google, and see they can use the "new" in jni. However I haven't found the solution ! If you can provide me the solution to use the new or it can't use ? – Hien Nguyen May 21 '13 at 10:01
  • @Zharf May you help me ? I don't know my solution for converting byte[] in java to C++ is good or not, and I'm searching the vice versa solution ! Please help me if you can ! – Hien Nguyen May 21 '13 at 10:09
  • The compiler message which you have just removed showed clearly that you are using C++ syntax with a C compiler. You can't do it. It doesn't make sense. All your edit accomplishes is removal of all the useful information. What's left is not a real question. – user207421 May 21 '13 at 10:26
  • :) I think I found a solution for that, thanks for your concern. – Hien Nguyen May 21 '13 at 11:16

2 Answers2

81

You can use this to convert unsigned char array into a jbyteArray

jbyteArray as_byte_array(unsigned char* buf, int len) {
    jbyteArray array = env->NewByteArray (len);
    env->SetByteArrayRegion (array, 0, len, reinterpret_cast<jbyte*>(buf));
    return array;
}

to convert the other way around...

unsigned char* as_unsigned_char_array(jbyteArray array) {
    int len = env->GetArrayLength (array);
    unsigned char* buf = new unsigned char[len];
    env->GetByteArrayRegion (array, 0, len, reinterpret_cast<jbyte*>(buf));
    return buf;
}
Zharf
  • 2,638
  • 25
  • 26
  • how about the below solution, is it different than yours ? What's better one? `jbyte* content_array = (*env)->GetByteArrayElements(env, array, NULL); //if (content_array == NULL) //return 0; ReceiveDataFromServer(content_array, array_length); (*env)->ReleaseByteArrayElements(env, array, content_array, JNI_ABORT); ` – Hien Nguyen May 21 '13 at 11:17
  • 2
    That will work pretty much the same. The difference is that in your code you provide the native code a buffer to put data into, in my version the native code creates the buffer. Which is better depends on the use case. – Zharf May 21 '13 at 11:36
  • @Zharf, how is "buf" get freed in as_unsigned_char_array()? – ransh Jun 14 '15 at 10:38
  • @ransh it isn't, it needs to be deleted manually after it's not needed anymore. Smart pointers might be preferred by some for this, of course. – Zharf Jun 15 '15 at 08:36
  • I've tried to pass large byte array (about 1.5 Mb) into as_unsigned_char_array(jbyteArray), and it causes a crash with a message "Fatal signal 11 (SIGSEGV), code 2, fault addr 0x9f235000 in tid 12821 (Thread-34144)". – G. Kh. Sep 02 '15 at 09:04
  • 1
    Will it work with const char*, instead of unsigned char*? – getsadzeg Jun 28 '16 at 13:36
  • The best answer to converting an uchar array to byte array in stackoverflow. – 김선달 Feb 26 '20 at 01:37
-1

The array buf is a variable on stack. After leaving the function is this variable undefined.

A solution is separating this function in two, one to compute the size and another with a pointer to now the allocated array as parameter to fill in the values.