How to find out endianness of a file?

*爱你&永不变心* 提交于 2019-12-13 16:12:38

问题


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?


回答1:


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').




回答2:


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.



来源:https://stackoverflow.com/questions/5187469/how-to-find-out-endianness-of-a-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!