Mac OS X 上 UniChar 的正确 JNA 映射是什么?
我有一个像这样的 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[],并使其正确转换吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不这么认为,基本上是因为 char 是解码的字节序列。
这就是为什么你的字节版本像手动解码一样工作
如果你想坚持使用字符,我建议:
不幸的是,我不知道执行这两者中任何一个的简单方法。
我的意见:坚持使用
byte[]
版本顺便问一下,您是如何创建
UInt16
类的?I dont think so basically because a char is a decoded byte sequence.
That's why your byte version works like a charm with the manual decoding
If you want to stick with chars I suggest that:
Unfortunalty I don't know an easy way to do any of the two.
My opinion : stick with the
byte[]
verionBy the way how did you create your
UInt16
class ?