1

I am using SWIG to generate an interface to some C code which I have compiled as an Android NDK library. My C code NDK library uses a structure, MY_STRUCT that contains char* elements that pass data in both the in and out directions and the SWIG generated JNI works fine as far as that goes, ie I can read the strings in the C code, set data and read the result in the Java code as required.

However I have a problem in that if I pass in a java String that contains null bytes the nulls are replaced by "\300\200"

Eg, if I create a string in java as follows :

MY_STRUCT myStruct = new MY_STRUCT();
byte[] myBytes = new byte[21];
String myString = new String(myBytes);
myStruct.setName(myString);

then myStruct has it's name field set to 21 null bytes as required and this is visible in the java debugger, but the string passed across to my NDK library code as seen in the NDK C debugger is as follows :

"\300\200\300\200\300\200\300\200\300\200\300\200\300\200\300\200\300\200\300\200\300\200\300\200\300\200\300\200\300\200\300\200\300\200\300\200\300\200\300\200\300\200"

My SWIG .i file has the following relevant portion :

%include various.i
%include cpointer.i
%pointer_functions(int, intp);

%include ../../../mydir/myheader.h

myheader.h has the following relevant portion :

typedef struct
{
...
    char* name;
...
} *P_MY_STRUCT, MY_STRUCT;

The C code debugs fine and I can read and write all the strings pointed to by the name etc elements of MY_STRUCT, the only issue is the transformation of null elements of the strings input to the SWIG generated JNI code into the strange "\300\200" elements in the C code in the NDK library.

EDIT:

Considering an alternative : I have several functions that take byte arrays instead of strings for C-function char* arguments and this is achieved in the myModule.i file as follows :

bool myFunc(P_MY_STRUCT, char* BYTE, int32_t);

Is there any way in SWIG of achieving the equivalent of the BYTE functionality for structure members ? I tried using the same BYTE trick in myModule.i as follows but it didn't work :

typedef struct
{
...
    char* BYTE;
...
} *P_MY_STRUCT, MY_STRUCT;

Thanks again,

Boo
  • 165
  • 2
  • 11

1 Answers1

0

This is known crazy behavior by Java and described in the answers to this question:

What does it mean to say "Java Modified UTF-8 Encoding"?

Community
  • 1
  • 1
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • Thanks for the interesting pointer, but there was nothing there to say how to deal with the issue ? Is there a way to override the encoding so as to use standard UFT-8 ? – Boo Oct 09 '14 at 07:12
  • I think you'd need to do the conversion from String to a byte array in Java with actual UTF-8 encoding then pass the byte array, rather than the string, to C. But I'm not a Java programmer so I'm not sure right off how you'd do this. – R.. GitHub STOP HELPING ICE Oct 09 '14 at 15:21