【问题标题】:JNA: How can I define a structure with fields of custom bit sizes?JNA:如何定义具有自定义位大小字段的结构?
【发布时间】:2016-07-15 19:41:26
【问题描述】:

例如,我有以下 C++ 结构...

struct dleaf_t
{
    int                 contents;           // OR of all brushes (not needed?)
    short               cluster;            // cluster this leaf is in
    short               area : 9;           // area this leaf is in
    short               flags : 7;          // flags
    short               mins[ 3 ];          // for frustum culling
    short               maxs[ 3 ];
    unsigned short      firstleafface;      // index into leaffaces
    unsigned short      numleaffaces;
    unsigned short      firstleafbrush;     // index into leafbrushes
    unsigned short      numleafbrushes;
    short               leafWaterDataID;    // -1 for not in water

    //!!! NOTE: for maps of version 19 or lower uncomment this block
    /*
    CompressedLightCube ambientLighting;    // Precaculated light info for entities.
    short           padding;        // padding to 4-byte boundary
    */
};

通常我可以使用 JNA 在 Java 中轻松表示结构,但这种结构使用自定义位数来表示数据类型(如果我没记错的话)。

例如,area9 位的 short,而不是像往常一样的 16 位。 flags7 位的短...等等。

如何定义具有自定义位大小数据类型的 JNA 结构?

【问题讨论】:

    标签: java struct structure jna bits


    【解决方案1】:

    将两个位字段合并为一个int字段,然后编写成员方法提取已合并的各个字段的值,例如

    public int area_flags;
    public int getArea() { return area_flags & 0x1FF; }
    public int getFlags() { return (area_flags >> 9) & 0x3F; }
    

    根据您的编译器如何打包这些位,您可能需要尝试一下。

    【讨论】:

      【解决方案2】:

      我不知道 JNA 是否可以做到这一点。请查看文档,看看是否可以找到有关您的问题的信息。

      【讨论】:

        猜你喜欢
        • 2022-01-12
        • 1970-01-01
        • 1970-01-01
        • 2021-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多