What is the correct JNA mapping for UniChar on Mac OS X?

Posted by Trejkaz on Stack Overflow See other posts from Stack Overflow or by Trejkaz
Published on 2010-10-20T01:13:02Z Indexed on 2010/12/24 14:54 UTC
Read the original article Hit count: 333

Filed under:
|
|
|

I have a C struct like this:

struct HFSUniStr255 {
    UInt16 length;
    UniChar unicode[255];
};

I have mapped this in the expected way:

public class HFSUniStr255 extends Structure
{
    public UInt16 length; // UInt16 is just an IntegerType with length 2 for convenience.

    public /*UniChar*/ char[] unicode = new char[255];
    //public /*UniChar*/ byte[] unicode = new byte[255*2];
    //public /*UniChar*/ UInt16[] unicode = new UInt16[255];

    public HFSUniStr255()
    {
    }

    public HFSUniStr255(Pointer pointer)
    {
        super(pointer);
    }
}

If I use this version, I get every second character of the string into my char[] ("aits D" for "Macintosh HD".) I am assuming that this is something to do with being on a 64-bit platform and JNA mapping the value to a 32-bit wchar_t but then chopping off the high 16 bits on each wchar_t on copying them back.

If I use the byte[] version, I get data which decodes correctly using the UTF-16LE charset.

If I use the UInt16[] version, I get the right code point for each character but it is then inconvenient to convert them back into a string.

Is there some way I can define my type as char[], and yet have it convert correctly?

© Stack Overflow or respective owner

Related posts about java

Related posts about unicode