I have written some code which passes values between java and c using jni.
Currently all numerics are defined as int (java) -> jnit (jni/c) -> unsigned int (c)
The code works but it is REALLY inefficient because not all the numbers being pass need the memory available to an integer.
I have 3 types of values in my code which need to hold number ranges 0 to 4294967295, 0 to 255 and 0 to 1.
I can’t work out compatible data types across all 3 “languages”.
Range Java C/JNI C
4294967296 int jint unsigned int
256 ??? ??? unsigned char
2 boolean jboolean ???
Can you please advise what data types I need to use for the ???s above?
Thanks
G
Remember, there are no unsigned types in Java. So a Java
intis not actually going to be able to store all the values of a Cunsigned int. You’re also operating on the assumption that a Cunsigned intis always 32 bits wide. It can be a different size, though the industry has somewhat standardized on it.With that out of the way, following the logic you have here,
intis tojintis tounsigned intasbyteis tojbyteis tounsigned char, and asbooleanis tojbooleanis to_Bool(an underused C99 type that can only hold1or0as a value.)Note that a
charin C is not the same as acharin Java; the former represents a single byte, however many bits wide it may be, while the latter represents a UTF-16 character. Also note that acharmay be signed or unsigned in C depending on the compiler and platform, so to be safe you should explicitly usesigned charif the sign might matter.