我需要读取包含此结构的二进制文件:
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();
}
前两个值(魔术和版本)是正确的但是对于第三个值我已经得到0这是错误的。
用Hex Editor打开文件(第一个字节,而不是整个文件):
以下是十六进制编辑器的输出:
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需要多少字节
有什么建议吗?
答案 0 :(得分:0)
使用DataInputStream
和readUnsignedByte
,readUnsignedShort
,readUTF
等来创建结构。
您可以使用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;
答案 1 :(得分:0)
如评论所述,您可能希望尝试使用Unsigned32来表示Java类中的版本和其他C int而不是Unsigned8,因为C int应该是4个字节而Unsigned8只代表1个字节。