Gün Karagöz
Gün Karagöz

Reputation: 765

How to find out endianness of a file?

How can I figure out whether it's a big-endian or little-endian file? I just tried to write a big-endian file with matlab but probably it didn't work. Now I want to learn if it is possible to learn what type it is. Any suggestion?

Upvotes: 2

Views: 3677

Answers (2)

gnovice
gnovice

Reputation: 125864

There's no way in general to know whether a given data file was created using big-endian or little-endian byte formatting. You would need to know something about the type of file it is (if it has a standard format that only ever uses one or the other) or the type of system the file was created on.

When you use fopen to open a file in MATLAB, you have to specify which endian format to use for that file using the machinefmt input argument. If you don't specify a format, MATLAB will use the native machine format by default (which won't be correct for files created on machines with different endian formats). This usage of fopen:

[filename, permission, machinefmt] = fopen(fileID);

Will simply tell you what endian format you used to open the file in the first place, which may or may not be correct for the given file.

Upvotes: 6

Jonas
Jonas

Reputation: 74940

Use FOPEN:

fileID = fopen(fileName)
[filename, permission, machineformat] = fopen(fileID)

The third output, machineformat, tells you whether it's big endian ('b') or little endian ('l').

Upvotes: 2

Related Questions