C#相当于什么解包结构(“21”,日志文件.read(8) )?

2024-04-27 04:03:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我没有太多直接处理二进制文件的经验(“我现在正在努力自学”),我正在使用python示例。我知道如何用C读入二进制文件,但我不太明白在这个python位中发生了什么:

 headerparts = struct.unpack("2l", logfile.read(8))
 headerlen = headerparts[1] - headerparts[0]
 header = struct.unpack(str(headerlen)+"l", logfile.read(headerlen*4))

我假设它是以8个字节读取数据,然后用它来计算头的长度……我不太确定。在

Python文档说明了这一点解包结构公司名称:

解包结构(fmt,字符串) 根据给定的格式解压缩字符串(可能是pack(fmt,…)打包的。结果是一个元组,即使它只包含一个项。字符串必须正好包含格式所需的数据量(len(string)必须等于calcsize(fmt))。在

我不太明白“21”是什么意思。这意味着它是一种格式,但我没有看到一个表来描述“21”是什么,我也不知道它是否意味着别的什么。有人能解释一下这是怎么回事吗?或者给我看看C的等价物吗?在


Tags: 文件字符串示例read格式二进制经验结构
2条回答

这就是我在C#的结局:

// get bytes from file
var fileBytes = File.ReadAllBytes("parseMe.log");

// figure out the header length
var bodyLength = BitConverter.ToInt32(fileBytes, 0);
var headerLength = BitConverter.ToInt32(fileBytes, 4) - bodyLength;

// capture the header, which is an array of end index offsets
var header = new int[headerLength];

// grab the header out of the file
for (var index = 0; index < headerLength; ++index)
    header[index] = BitConverter.ToInt32(fileBytes, 8 + index * 4);

它不是21,而是2l,意思是两个l。在

l表示long(参见^{} - Format characters

相关问题 更多 >