title: Reading 2's complement (32-bit int) from a binary file
body: <p>Ok, first of, hello everyone :)
  I'm running Python 3.2 x86 @ windows</p>
  
  <p>So I've got a binary file (6 412 bytes) with the following structure:</p>
  
  <pre><code>header: 256 bytes
  maxZ: 4 bytes (1 32-bit word)
  Zvalues: 2048 bytes (512 32-bit words)
  maxX: *same as maxZ*
  Xvalues: *same as Zvalues*
  maxY: *same as maxZ*
  Yvalues: *same as Zvalues*
  </code></pre>
  
  <p>I know (due to the format specs) that numbers are stored as 32-bit ints written as 2's complements</p>
  
  <p>Say I don't need to read the header, here's what I do (I've got 1539 4-byte samples to read):</p>
  
  <pre><code>import struct
  file = open('C:/02.adb', 'rb')
  file.seek(256)
  fmt = '&lt;i'
  nbytes = 4
  for i in range(1539):
      packed_data = file.read(nbytes)
      unpacked_data = struct.unpack(fmt, packed_data)
      print('Sample: ',i,'Value: ',unpacked_data[0])
  file.close()
  </code></pre>
  
  <p>That reads the file and prints it's contents to stdout, but there's something wrong with the signs. I guess I'm not reading numbers as 2's complements here... How do I improve the script?</p>
  
  <p>tnx for the attention</p>
  
  <p>addition (23.10.12):</p>
  
  <p>I realised that by writing that short version of a larger external script I've just solved the main problem itself — my own mindlessness. </p>
  
  <p>The number of samples that can be read from a header of the file isn't exact overall number of samples stored it in but the number of ZXY values combinations excluding those maximum amplitudes stored in the very first word of each channel's data block.</p>
  
  <p>here's the final short version of script and it's output (and YES, python representation of signed ints is '2's complement'):</p>
  
  <pre><code>import struct
  file = open('C:/02.adb', 'rb')
  file.seek(256)
  transfer_ratio = 31446.540881
  fmt = '&lt;i'
  nbytes = 4
  samples = []
  for i in range(1539):
      packed_data = file.read(nbytes)
      unpacked_data = struct.unpack(fmt, packed_data)
      samples.append(int(unpacked_data[0])/transfer_ratio)
  for i in range(32):
      print('Ch1_val: ',samples[i+1],' Ch2_val: ',samples[i+1+513],' Ch3_val: ',samples[i+1+(513*2)])
  file.close()
  </code></pre>
  
  <p>output:</p>
  
  <pre><code>Ch1_val:  42.20127119933322  Ch2_val:  36.47237399942374  Ch3_val:  -3.4996535999447054
  Ch1_val:  41.37017819934635  Ch2_val:  36.64441199942102  Ch3_val:  -4.35822179993114
  Ch1_val:  42.43585979932951  Ch2_val:  34.9874729994472  Ch3_val:  -4.125827399934812
  Ch1_val:  44.08278179930349  Ch2_val:  34.42912859945602  Ch3_val:  -4.1604257999342655
  Ch1_val:  44.50289159929685  Ch2_val:  35.507053199438985  Ch3_val:  -3.092836199951133
  Ch1_val:  43.69110119930968  Ch2_val:  35.53335179943857  Ch3_val:  -2.613991799958699
  Ch1_val:  42.73662419932476  Ch2_val:  34.1708807994601  Ch3_val:  -5.254886399916972
  Ch1_val:  42.14097839933417  Ch2_val:  34.08460739946146  Ch3_val:  -8.582724599864394
  Ch1_val:  41.982105599336684  Ch2_val:  35.58544019943775  Ch3_val:  -8.84688719986022
  Ch1_val:  42.503816399328436  Ch2_val:  36.06170879943022  Ch3_val:  -6.777056999892922
  Ch1_val:  43.17568679931782  Ch2_val:  35.291926199442386  Ch3_val:  -4.984618199921243
  Ch1_val:  42.4303583993296  Ch2_val:  36.03750899943061  Ch3_val:  -4.502275799928864
  Ch1_val:  39.92264219936922  Ch2_val:  38.354075399394006  Ch3_val:  -6.050744999904398
  Ch1_val:  37.65501599940505  Ch2_val:  38.48181599939198  Ch3_val:  -9.010943399857627
  Ch1_val:  37.04642759941466  Ch2_val:  35.152642199444585  Ch3_val:  -9.89107199984372
  Ch1_val:  36.571876199422164  Ch2_val:  32.21988719949093  Ch3_val:  -7.850465999875962
  Ch1_val:  34.488403799455085  Ch2_val:  32.62164839948458  Ch3_val:  -7.712485799878142
  Ch1_val:  32.00259779949436  Ch2_val:  34.05570119946192  Ch3_val:  -11.215064999822802
  Ch1_val:  31.132422599508107  Ch2_val:  33.44755799947153  Ch3_val:  -12.461179799803112
  Ch1_val:  31.245853199506314  Ch2_val:  31.730771399498654  Ch3_val:  -8.723057999862176
  Ch1_val:  30.40820939951955  Ch2_val:  31.03355639950967  Ch3_val:  -6.433076399898357
  Ch1_val:  28.474165199550107  Ch2_val:  30.714824999514704  Ch3_val:  -9.730672799846255
  Ch1_val:  26.481449999581592  Ch2_val:  29.53720739953331  Ch3_val:  -13.227496199791005
  Ch1_val:  24.79611359960822  Ch2_val:  28.60448159954805  Ch3_val:  -12.1075955998087
  Ch1_val:  23.74938479962476  Ch2_val:  28.95253259954255  Ch3_val:  -9.411305399851301
  Ch1_val:  23.92663799962196  Ch2_val:  29.24305739953796  Ch3_val:  -8.856490799860067
  Ch1_val:  24.37285559961491  Ch2_val:  28.72398599954616  Ch3_val:  -9.611549999848137
  Ch1_val:  23.155774199634138  Ch2_val:  29.22521759953824  Ch3_val:  -10.701304199830918
  Ch1_val:  20.822671799671003  Ch2_val:  31.46641799950283  Ch3_val:  -12.57088979980138
  Ch1_val:  20.15302739968158  Ch2_val:  32.95071479947938  Ch3_val:  -13.581175799785417
  Ch1_val:  21.440195999661245  Ch2_val:  32.28628559948987  Ch3_val:  -11.66643419981567
  Ch1_val:  21.51502139966006  Ch2_val:  31.7910959994977  Ch3_val:  -8.843802599860268
  </code></pre>