binary files to text
17 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I'm having a hard time grasping how to deal with binary files. I need to convert and manipulate in .txt. Can someone suggest a good tutorial on how to perform various binary to txt conversions. I've looked at the help files and for some reason things are just not quite clicking...
Thanks
2 Kommentare
Image Analyst
am 7 Nov. 2011
ALL files on a computer are binary if you think about it, even files that are already text.
http://www.mathworks.com/matlabcentral/answers/6200-tutorial-how-to-ask-a-question-on-answers-and-get-a-fast-answer
Akzeptierte Antwort
Walter Roberson
am 7 Nov. 2011
First step: fopen() the file and assign the resulting file identifier to a variable
Second step: use fread() specifying the file identifier to read from, and the number of items to be read, and specifying a conversion string. The conversion string can be in three related forms:
'A'
'A=>B'
'*A'
such as 'uint8', 'int16=>single', '*int32'
In each case the first (or only) type name indicates how the data is stored in binary in the file: for example, 'uint8' indicates that a single 8 bit unsigned integer is to be read from the file.
If the type name is preceded by '*' in the type specification, such as the '*int32' example I gave, then the value that is created by fread() to return to the workspace is to have exactly the same data type as was in the file.
If instead the type name is followed by by '=>' and a second type specification, then the value is read from file according to the first type specification, but is then to undergo data type conversion to the second data type. In my example above, 'int16=>single', the input would be read as a value if datetype int16, and then single() would be applied to that, converting the data to a single precision number.
If the type specification is a single type name with no '*' and no '=>' then the input will be read as a value in the data type specified, and it will then undergo data type conversion to type 'double'.
There are bunch of data types described on the fread() documentation page. The types you are likely to observe most often in files are uint8, int32, double, and char.
You will want to use the '*' "preserve the data type" option most of the time, with using a '=>' conversion probably being second most common. The majority of the time if you see a type by itself used in fread, chances are that it is a program bug with the programmer having expected that the output type will be the same as the input type. Because of this, in the cases where you really do want the value converted to double precision, it is best to specifically use '=>double' which might be more typing but will save a lot of time in understanding what the program is doing.
Third step: fclose() the file identifier after you are finished with it.
Converting binary (numeric values) to text has so many different options that it is best to read through the fprintf() documentation page.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu String Parsing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!