1

I have created the following helper function but I am unable to get it to generate a java interface that accepts a byte array as the input.

size_t get_p_wchar_t_bytes(wchar_t *wstr, char * bytes, size_t len)
{
    size_t wlen,cpylen;
    wlen = count_p_wchar_t_bytes(wstr);
    cpylen = len > wlen ? wlen : len;
    memcpy(bytes, wstr, cpylen);
    return cpylen;
}

size_t count_p_wchar_t_bytes(wchar_t *wstr)
{
    return wcslen(wstr) * sizeof(wchar_t);
}

I have the following defined in my template

%include "arrays_java.i"
%apply int[] {int *};
%include "wchar.i"

The problem with this is that the char * is being converted to a "String" instead of a byte array which isn't helpful.

public static long get_p_wchar_t_bytes(SWIGTYPE_p_wchar_t wstr, String bytes, long len)

What I want as the output from swig is this...

public static long get_p_wchar_t_bytes(SWIGTYPE_p_wchar_t wstr, byte[] bytes, long len)

So how can I do this? Ultimately I simply need a way to pass a w_char_t array to java in a format it can work with without creating any memory leaks.

3
  • Why do you want to do wchar_t to byte? Sounds like you're trying to solve the wrong problem. Commented Apr 23, 2014 at 6:26
  • Flexo I have another function which returns sizeof(wchar_t) so with that information I can determine if the encoding is UTF-16 or UTF-32 for conversion of a byte array into a Java String. Commented Apr 23, 2014 at 7:05
  • If you just want to make it work properly as a Java string you're making this far more complicated than needed. Commented Apr 23, 2014 at 7:07

1 Answer 1

1

I was able to solve this problem by changing my template to the following which is a solution I found at http://swig.10945.n7.nabble.com/Converting-char-to-byte-String-in-Java-td276.html

// knows about things like int *OUTPUT:
%include "typemaps.i"
// knows about int32_t
%include "stdint.i"
%include "arrays_java.i"
%apply int[] {int *};

// convert char * to byte array
%apply signed char[] {char* pchar}; 

%include "wchar.i"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.