Search code examples
javacarraystypesjna

How do I convert 16-bit char array to 8-bit char array and send to c code using JNA?


I have a function in my c code, whose definition is below:

void myfunct(double *xx, double *yy, double *si, double *ded, double *limit, double *al, double *bl, double *cl, int ndat, const char *path, int plenght, int error,int pgamodell,int converter, double backnoise);

When I execute this c program in gcc, it executes and gives me the required output. (It is working fine!)

Now, my task is to use this c code in java, so for it I am using JNA. But I am having a problem with the path parameter of this function. On my java side I am sending this parameter in the following manner:

String p = "E:/Development/dir/";
char[] path = p.toCharArray();

// and sending this path in the function call

But on the c program, I have debugged and found that it gets path with only one character i.e "path=E".

I guess the reason is that in java char array is 16-bit but in c it is 8-bit.

Kindly help me in solving this problem as I am new to JNA, thanks in advance for your time.


Solution

  • If your native signature requires const char*, then JNA will automagically work when you pass a Java String. No conversion necessary.

    Internally, JNA will create a temporary buffer (for the duration of the call) and encode the Java String as a native NUL-terminated byte array (defaults to utf8 encoding, but can be modified by setting the system property jna.encoding).