gpt4 book ai didi

java - 数据输入流读取的数据与所需的 int 值不同

转载 作者:行者123 更新时间:2023-12-01 10:17:22 25 4
gpt4 key购买 nike

我正在尝试使用 DataInputStream 读取 .dem(反恐精英演示文件)文件。到目前为止,我知道它以 header 开头,在 C 中它看起来像这样 ( https://github.com/csgo-data/demoinfogo-linux/blob/master/src/demofile.h )

struct demoheader_t
{
char demofilestamp[ 8 ]; // Should be HL2DEMO
int32 demoprotocol; // Should be DEMO_PROTOCOL
int32 networkprotocol; // Should be PROTOCOL_VERSION
char servername[ MAX_OSPATH ]; // Name of server
char clientname[ MAX_OSPATH ]; // Name of client who recorded the game
char mapname[ MAX_OSPATH ]; // Name of map
char gamedirectory[ MAX_OSPATH ]; // Name of game directory (com_gamedir)
float playback_time; // Time of track
int32 playback_ticks; // # of ticks in track
int32 playback_frames; // # of frames in track
int32 signonlength; // length of sigondata in bytes
};

,这是我的头类,它负责从流中读取它:

package cropper;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintStream;

import static cropper.Cropper.*;

public class DemoHeader {

private final static int MAX_OSPATH = 260;
private final static int STAMP_L = 8;

char demofilestamp[]; // Should be HL2DEMO
int demoprotocol; // Should be DEMO_PROTOCOL
int networkprotocol; // Should be PROTOCOL_VERSION
char servername[]; // Name of server
char clientname[]; // Name of client who recorded the game
char mapname[]; // Name of map
char gamedirectory[]; // Name of game directory (com_gamedir)
float playback_time; // Time of track
int playback_ticks; // # of ticks in track
int playback_frames; // # of frames in track
int signonlength; // length of sigondata in bytes

private DemoHeader(){
demofilestamp = new char[STAMP_L];
servername = new char[MAX_OSPATH];
clientname = new char[MAX_OSPATH];
mapname = new char[MAX_OSPATH];
gamedirectory = new char[MAX_OSPATH];
}

public static DemoHeader getHeader(DataInputStream dis) throws IOException{
DemoHeader header = new DemoHeader();
for (int i = 0; i < header.demofilestamp.length; header.demofilestamp[i++] = (char) dis.readByte());
header.demoprotocol = dis.readInt();
header.networkprotocol = dis.readInt();
for (int i = 0; i < header.servername.length; header.servername[i++] = (char) dis.readByte());
for (int i = 0; i < header.clientname.length; header.clientname[i++] = (char) dis.readByte());
for (int i = 0; i < header.mapname.length; header.mapname[i++] = (char) dis.readByte());
for (int i = 0; i < header.gamedirectory.length; header.gamedirectory[i++] = (char) dis.readByte());
header.playback_time = dis.readFloat();
header.playback_ticks = dis.readInt();
header.playback_frames = dis.readInt();
header.signonlength = dis.readInt();
return header;
}

public void println(){
PrintStream p = System.out;
p.print("Filestamp - ");
for (int i = 0; i < 8; p.print(demofilestamp[i++]));
p.println();
p.println("Demo protocol - " + Integer.toUnsignedString( demoprotocol ));
p.println("Network protocol - " + networkprotocol);
p.print("Server name - ");
for (int i = 0; i < servername.length; p.print(servername[i++]));
p.println();
p.print("Client name - ");
for (int i = 0; i < clientname.length; p.print(clientname[i++]));
p.println();
p.print("Map name - ");
for (int i = 0; i < mapname.length; p.print(mapname[i++]));
p.println();
p.print("Game directory - ");
for (int i = 0; i < gamedirectory.length; p.print(gamedirectory[i++]));
p.println();
p.println("Play back time - " + playback_time);
p.println("Play back ticks - " + playback_ticks);
p.println("Play back frames - " + playback_frames);
p.println("Sign on length - " + signonlength);

}

}

读取随机 .dem 文件的 header 并打印其结果

Filestamp - HL2DEMO
Demo protocol - 67108864
Network protocol - -684457984
Server name - eBot :: ENVYUS vs E-Frag.net
Client name - GOTV Demo
Map name - de_cache
Game directory - csgo
Play back time - 1.9518368E-38
Play back ticks - 1084033024
Play back frames - 345769984
Sign on length - -544012032

这对于所有数字都是错误的(例如 demoprotocol 必须是 4)。我做错了什么?

最佳答案

我下载了a random .dem file来自网络。使用您的代码我得到以下输出:

Filestamp - HL2DEMO
Demo protocol - 67108864
Network protocol - -801898496
Server name - eBot :: mousesports vs E-Frag.net
Client name - GOTV Demo
Map name - de_cache
Game directory - csgo
Play back time - 6.5734844E19
Play back ticks - 595788800
Play back frames - -1451097088
Sign on length - -1433729280

请注意我如何获得与您相同的演示协议(protocol)编号。

所以我在十六进制编辑器中打开了该文件。前 12 个字节如下所示:

48 4C 32 44 45 4D 4F 00 04 00 00 00

前 8 个字节 (48 4C 32 44 45 4D 4F 00) 形成字符串 HL2DEMO(加上终止空字节 \0 )。但请注意接下来的 4 个字节:04 00 00 00。这是整数值 4,但在 Little Endian 中。 Java 的 DataInputStream 将 int 读取为 Big Endian。

使用 Guava 的 LittleEndianDataInputStream我得到以下输出,而不是 Java 的 DataInputStream:

Filestamp - HL2DEMO
Demo protocol - 4
Network protocol - 13520
Server name - eBot :: mousesports vs E-Frag.net
Client name - GOTV Demo
Map name - de_cache
Game directory - csgo
Play back time - 2310.2734
Play back ticks - 295715
Play back frames - 295593
Sign on length - 494506

关于java - 数据输入流读取的数据与所需的 int 值不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35812202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com