3

次のような C 構造体があります。

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

これを予想どおりにマッピングしました:

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?

4

1 に答える 1

0

char はデコードされたバイトシーケンスであるため、基本的にそうは思いません。

それが、バイトバージョンが手動デコードで魅力的に機能する理由です

文字に固執したい場合は、次のことをお勧めします。

  • JNAをデコーダーでフックします
  • または、取得した UTF-16LE から chars 数値形式を unicode である内部 JVM charset に変換します

残念ながら、この 2 つのいずれかを行う簡単な方法はわかりません。

私の意見:バージョンに固執byte[]する


ところで、どのようにUInt16クラスを作成しましたか?

于 2010-12-24T14:46:28.387 に答える