From: utab on
Dear all,

I am trying to read the binary files written by a commercial program,
however I ran into a problem somewhere. Directly using fread to read one
integer from the file, works fine. However if I do the same with C
language I should do byte swapping because it seems that the files are
written MSB first, this is also a bit strange to me because the machine
where these binary files are written is little-endian but the program
seems to order the bytes in the other way. The question is that: has
MATLAB some special checking for this operation, so it automatically
swaps the bytes, and if yes, how could that understand that it should
swap bytes in the original file to be able to read sth sane.

Best,
U.
From: dpb on
utab wrote:
> Dear all,
>
> I am trying to read the binary files written by a commercial program,
> however I ran into a problem somewhere. Directly using fread to read one
> integer from the file, works fine. However if I do the same with C
> language I should do byte swapping because it seems that the files are
> written MSB first, this is also a bit strange to me because the machine
> where these binary files are written is little-endian but the program
> seems to order the bytes in the other way. The question is that: has
> MATLAB some special checking for this operation, so it automatically
> swaps the bytes, and if yes, how could that understand that it should
> swap bytes in the original file to be able to read sth sane.

Matlab "assumes" the word order is consistent with the natural order on
the platform on which it is running.

You can handle foreign files with non-native ordering thru the options
in the fopen() call but there's nothing specific done different
automagically, no.

--
From: Steven Lord on

"dpb" <none(a)non.net> wrote in message
news:hvamvv$6qq$1(a)news.eternal-september.org...
> utab wrote:
>> Dear all,
>>
>> I am trying to read the binary files written by a commercial program,
>> however I ran into a problem somewhere. Directly using fread to read one
>> integer from the file, works fine. However if I do the same with C
>> language I should do byte swapping because it seems that the files are
>> written MSB first, this is also a bit strange to me because the machine
>> where these binary files are written is little-endian but the program
>> seems to order the bytes in the other way. The question is that: has
>> MATLAB some special checking for this operation, so it automatically
>> swaps the bytes, and if yes, how could that understand that it should
>> swap bytes in the original file to be able to read sth sane.
>
> Matlab "assumes" the word order is consistent with the natural order on
> the platform on which it is running.
>
> You can handle foreign files with non-native ordering thru the options in
> the fopen() call but there's nothing specific done different
> automagically, no.

utab,

If for some reason you can't handle this through the machineformat input
argument to FOPEN (which is what dpb suggested), you could use SWAPBYTES
(assuming you have a sufficiently recent version of MATLAB.)

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fopen.html

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/swapbytes.html

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com


From: utab on
On Wed, 16 Jun 2010 09:24:34 -0500, dpb wrote:

> Matlab "assumes" the word order is consistent with the natural order on
> the platform on which it is running.
>
> You can handle foreign files with non-native ordering thru the options
> in the fopen() call but there's nothing specific done different
> automagically, no.

Then there is still sth fishy because I should swap bytes in C to get the
right information, with native C standard library functions and as far as
I know MATLAB also uses these functions, but you are right that it should
be consistent on the same system, however it does not seem so.
From: utab on
On Wed, 16 Jun 2010 10:37:23 -0400, Steven Lord wrote:


> utab,
>
> If for some reason you can't handle this through the machineformat input
> argument to FOPEN (which is what dpb suggested), you could use SWAPBYTES
> (assuming you have a sufficiently recent version of MATLAB.)
>
> http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fopen.html
>
> http://www.mathworks.com/access/helpdesk/help/techdoc/ref/swapbytes.html

Hi Steven,
the point is that fread gets the right information, using a C program
does not result in the right information without swapping bytes that is
the source of the question.
Best,
Umut