2012-05-03 60 views
5

我正在分析一组python脚本并遇到此代码段。我不确定我的解释是否正确,因为我没有遇到类似的C或Java代码,也不知道Python。验证我对此python代码段的解释是否正确

for i in xrange(self.num_sections): 
     offset, a1,a2,a3,a4 = struct.unpack('>LBBBB', self.data_file[78+i*8:78+i*8+8]) 
     flags, val = a1, a2<<16|a3<<8|a4 
     self.sections.append((offset, flags, val)) 

我的理解是这样的:

for each item in num_sections 
    convert the data_file range into a big-endian unsigned long, and 4 unsigned char 
    insert unpacked values into offset, a1, a2, a3 and a4 variables 

    set flags to = a1 
    set val to a2 shifted left 16 bits then OR'd with a3 shifted right 8 bits 
    then OR'd with a4 

从本质上讲,我觉得原来的解压缩操作中提取8个字节,转储其中4个为一个unsigned long,然后添加按顺序到休息一个*变量。

+3

这似乎是完全正确的(除了一个错字?a3左移,而不是右)。 –

+0

看起来很适合我。除了@NiklasB的左/右。找到。 –

+0

是。所以它基本上是一个4字节的int,一个字节和一个3字节的int。 –

回答

0

是的,你的解释是正确的。