Can I cast native primitive type into a JNI primitive type without worrying about endianness?

201 views Asked by At

Let's say I have

const uint16_t n = 0x0001;

Then can I cast like this?

const jint j = (jint) n;

Without worrying about endianness of the native platform?

Supplement

I have a function changes a value into an char array.

char * value_to_array(void * value, const size_t size) {
  char * array = malloc(size);
  if (array != NULL) {
    memcpy(array, value, size);
  }
  return array;
}

Now I should care about the endianness, right? What about the above simple cast?

1

There are 1 answers

2
user207421 On BEST ANSWER

Then can I cast like this?

Yes. JNI primitive types are machine-dependent.

Your second example preserves whatever endian-ness was present in the source, which you haven't specified.