title: Parsing a binary file written in MATLAB from Python and vice versa
body: <p>I am having  major troubles with <code>struct.unpack</code> in python. I have a binary file with a pre-determined format, that can either be written in MATLAB or in Python.</p>
  
  <p>I can write binary data to a file in Python and read the data back with no issues. I can also write the same data to a binary file from MATLAB and read it back in MATLAB with no problem. </p>
  
  <p>My problem comes when I either write the data from MATLAB and try to read it back in Python, or when I write the data in Python and try to read it back in MATLAB.</p>
  
  <p>For simplicity, let's say I'm writing two integers to a binary file (big-endian). Each integer is 4 bytes. The first integer is a valid integer not greater than 4 bytes, and the second integer must be equal to either 1, 2, or 3.</p>
  
  <p>First, here is how I write my data in MATLAB:</p>
  
  <pre><code>fid=fopen('hello_matlab.test','wb');
  first_data=4+4;
  second_data=1;
  
  fwrite(fid,first_data,'int');
  fwrite(fid,second_data,'int');
  
  fclose(fid);
  </code></pre>
  
  <p>And here is how I read that back in MATLAB:</p>
  
  <pre><code>fid=fopen('hello_matlab.test','rb');
  first_data=fread(fid,1,'int');
  second_data=fread(fid,1,'int');
  
  fprintf('first data: %d\n', first_data);
  fprintf('second data: %d\n', second_data);
  
  fclose(fid);
  
      &gt;&gt; first data: 8
      &gt;&gt; second data: 1
  </code></pre>
  
  <p>Now, here is how I write the data in Python:</p>
  
  <pre><code>fid=open('hello_python.test','wb')
  first_data=4+4
  second_data=1
  
  fid.write(struct.pack('&gt;i',first_data))
  fid.write(struct.pack('&gt;i',second_data))
  
  fid.close()
  </code></pre>
  
  <p>And here is how I read that data back in python. Also note, the commented out portion worked (when reading from files written in Python). I originally thought something weird was happening with the way <code>struct.calcsize('&gt;i')</code> was being calculated, so I removed it and instead put a hard-coded constant, <code>INTEGER_SIZE</code>, to represent the amount of bytes I knew MATLAB had used when encoding it:</p>
  
  <pre><code>INTEGER_SIZE=4
  
  fid=open('hello_python.test','rb')
  
  ### FIRST WAY I ORIGINALLY READ THE DATA ###
  # This works, but I figured I would try hard coding the size
  # so the uncommented version is what I am currently using.
  #
  # first_data=struct.unpack('&gt;i',fid.read(struct.calcsize('&gt;i')))[0]
  # second_data=struct.unpack('&gt;i',fid.read(struct.calcsize('&gt;i')))[0]
  
  ### HOW I READ DATA CURRENTLY ###
  first_data=struct.unpack('&gt;i',fid.read(INTEGER_SIZE))[0]
  second_data=struct.unpack('&gt;i',fid.read(INTEGER_SIZE))[0]
  
  print "first data: '%d'" % first_data
  print "second data: '%d'" % second_data
  
  fid.close()
  
      &gt;&gt; first data: 8
      &gt;&gt; second data: 1
  </code></pre>
  
  <p>Now, lets say I want to read <code>hello_python.test</code> in MATLAB. With my current MATLAB code, here is the new output:</p>
  
  <pre><code>&gt;&gt; first data: 419430400
  &gt;&gt; second data: 16777216
  </code></pre>
  
  <p>That is strange, so I did the reverse. I looked at what happens when I read <code>hello_matlab.test</code>. With my current Python code, here is the new output:</p>
  
  <pre><code>&gt;&gt; first data: 419430400
  &gt;&gt; second data: 16777216
  </code></pre>
  
  <p>So, something weird is happening but I don't know what it is. Also note, although this is part of a larger project, I did just extract these parts of my code into a new project, and tested the example above with those results. I'm really confused on how to make this file portable :( Any help would be appreciated.</p>