我有一个这样的 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);
}
}
如果我使用这个版本,我会将字符串的第二个字符放入我的 char[] (“aits D”代表“Macintosh HD”。)我假设这与在 64 位平台和 JNA 上有关将该值映射到 32 位 wchar_t,然后在将它们复制回来时将每个 wchar_t 上的高 16 位切掉。
如果我使用 byte[] 版本,我会得到使用 UTF-16LE 字符集正确解码的数据。
如果我使用 UInt16[] 版本,我会为每个字符获得正确的代码点,但是将它们转换回字符串会很不方便。
有什么方法可以将我的类型定义为 char[],然后让它正确转换?