[sapug] Problem with reading binary file (different result between MATLAB and Python)

Daryl Tester dt-sapug at handcraftedcomputers.com.au
Sun Jan 11 21:21:46 CET 2009

Hani - just letting you know that this email to sapug came from an
unsubscribed address, so you may not see the responses that are
sent back solely to the mailing list.

Hani Zahiri wrote:

> I am trying to translate one of my MATLAB scripts to Python and I am
> experiencing a strange problem (at least to me!) and I am desperetly
> looking for help. The binary file is a raw binary containing header
> information (first 720 bytes) following by radar data. For better
> illustration and using python basic functions, first 800 bytes of the
> file is look like this:


> '\x00\x00\x00\x012\xc0\x12\x12\x00\x00\x02\xd0A   CEOS-SAR-CCT A A 1.00


> ans =
>    720 


>>>> fid.seek(8)
>>>> scipy.fromfile(fid,'uint32',1)
> array([3489792000], dtype=uint32)

I don't know anything about scipy, but this is just an endian problem.
Given the binary string you're trying to read (which I'm doing some
interpreting here, as the seek doesn't quite marry up with the initial
string above):

>>> s = '\x00\x00\x02\xd0'
>>> import struct
>>> print struct.unpack('<I', s)
>>> print struct.unpack('>I', s)

It appears you need to unpack the data big-endian to get the number
that you want.  How Scipy copes with endian-ness is left as an
exercise to the recipient.  :-)

  Daryl Tester

"Oh Christmas tree, oh Christmas tree!  From hell's heart I stab at thee."
  -- A very Kaaahn! Christmas

More information about the sapug mailing list