I am having major troubles with struct.unpack
in python. I have a binary file with a pre-determined format, that can either be written in MATLAB or in Python.
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.
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.
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.
First, here is how I write my data in MATLAB:
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);
And here is how I read that back in MATLAB:
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);
>> first data: 8
>> second data: 1
Now, here is how I write the data in Python:
fid=open('hello_python.test','wb')
first_data=4+4
second_data=1
fid.write(struct.pack('>i',first_data))
fid.write(struct.pack('>i',second_data))
fid.close()
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 struct.calcsize('>i')
was being calculated, so I removed it and instead put a hard-coded constant, INTEGER_SIZE
, to represent the amount of bytes I knew MATLAB had used when encoding it:
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('>i',fid.read(struct.calcsize('>i')))[0]
# second_data=struct.unpack('>i',fid.read(struct.calcsize('>i')))[0]
### HOW I READ DATA CURRENTLY ###
first_data=struct.unpack('>i',fid.read(INTEGER_SIZE))[0]
second_data=struct.unpack('>i',fid.read(INTEGER_SIZE))[0]
print "first data: '%d'" % first_data
print "second data: '%d'" % second_data
fid.close()
>> first data: 8
>> second data: 1
Now, lets say I want to read hello_python.test
in MATLAB. With my current MATLAB code, here is the new output:
>> first data: 419430400
>> second data: 16777216
That is strange, so I did the reverse. I looked at what happens when I read hello_matlab.test
. With my current Python code, here is the new output:
>> first data: 419430400
>> second data: 16777216
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.