0

我需要读取包含此结构的二进制文件:

struct iqmheader
{
    char magic[16]; // the string "INTERQUAKEMODEL\0", 0 terminated
    uint version; // must be version 2
    uint filesize;
    uint flags;
    uint num_text, ofs_text;
    uint num_meshes, ofs_meshes;
    uint num_vertexarrays, num_vertexes, ofs_vertexarrays;
    uint num_triangles, ofs_triangles, ofs_adjacency;
    uint num_joints, ofs_joints;
    uint num_poses, ofs_poses;
    uint num_anims, ofs_anims;
    uint num_frames, num_framechannels, ofs_frames, ofs_bounds;
    uint num_comment, ofs_comment;
    uint num_extensions, ofs_extensions; 
};

有没有一种优雅的方式来做到这一点?我以这种方式尝试了 javolution lib:

import javolution.io.Struct;
import java.nio.ByteOrder;

public class IqmHeader extends Struct {
    public final UTF8String magic = new UTF8String(16);
    public final Unsigned8 version = new Unsigned8();
    public final Unsigned8 filesize = new Unsigned8();   
}

前 2 个值(魔法值和版本)是正确的,但对于第三个值,我已经得到 0,这是错误的。

使用十六进制编辑器打开的文件(第一个字节,不是整个文件):

这是十六进制编辑器的输出:

49 4E 54 45 52 51 55 41 4B 45 4D 4F 44 45 4C 00 02 00 00 00 10 EF 03 00 00 00 00 00 60 03 00 00 7C 00 00 00 02 00 00 00 DC 03 00 00 06 00 00 00 45 07 00 00 0C 04 00 00 AC 0B 00 00 A0 9B 01 00 B0 27 02 00 4B 00 00 00 C0 B3 02 00 4B 00 00 00 D0 C1 02 00 01 00 00 00 98 DB 02 00 65 00 00 00 4D 01 00 00 AC DB 02 00 70 E2 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 42 6F 64

我必须承认我真的不知道如何阅读这个。前 16 个字节是后面跟着“00”的字符串。然后是版本“02”-在这里我不知道 uint 需要多少字节

有什么建议么?

4

2 回答 2

0

正如评论的那样,您可能想尝试使用 Unsigned32 来表示 Java 类中的版本和其他 C int 而不是 Unsigned8,因为 C int 应该是 4 个字节,而 Unsigned8 只表示 1 个字节。

于 2013-07-30T22:43:22.013 回答
0

使用DataInputStreamand readUnsignedByte,readUnsignedShortreadUTF来创建结构。

您可以使用 Java 类来表示结构:

public class IQMHeader {
    String magic; // the string "INTERQUAKEMODEL\0", 0 terminated
    long version; // must be version 2
    long filesize;
    long flags;
    long num_text, ofs_text;
    long num_meshes, ofs_meshes;
    long num_vertexarrays, num_vertexes, ofs_vertexarrays;
    long num_triangles, ofs_triangles, ofs_adjacency;
    long num_joints, ofs_joints;
    long num_poses, ofs_poses;
    long num_anims, ofs_anims;
    long num_frames, num_framechannels, ofs_frames, ofs_bounds;
    long num_comment, ofs_comment;
    long num_extensions, ofs_extensions; 
};

您可以像这样读取 unsigned int :

long unsigned = input.readInt() & 0xffffffffL;
于 2013-07-30T21:51:44.810 回答