擅长:python、mysql、java
<p>读取二进制文件内容,如下所示:</p>
<pre><code>with open(fileName, mode='rb') as file: # b is important -> binary
fileContent = file.read()
</code></pre>
<p>然后使用<a href="http://docs.python.org/library/struct.html#struct.unpack">struct.unpack</a>“解包”二进制数据:</p>
<p>开始字节:<code>struct.unpack("iiiii", fileContent[:20])</code></p>
<p>正文:忽略标题字节和尾随字节(=24);其余部分构成正文,以知道正文中的字节数做整数除以4;所得的商乘以字符串<code>'i'</code>,以创建解包方法的正确格式:</p>
<pre><code>struct.unpack("i" * ((len(fileContent) -24) // 4), fileContent[20:-4])
</code></pre>
<p>结束字节:<code>struct.unpack("i", fileContent[-4:])</code></p>