how to read the data byte by byte in .raw file?
Afficher commentaires plus anciens
I have .raw file of size around 150MB and the data is stored in the form of hexadecimal , i want to read this data byte level and to display. I am giving here example of how data is stored in file.
00000 00 12 06 25 32 45 02 35 C2 00 B7 00 FD 00 4C 00
00010 63 00 B5 00 21 00 A3 00 61 00 D7 00 5A 00 25 00
00020 ---------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------
I am not able to read this file , suggest me how to read this file and display the contents .
thank you.
Réponses (2)
Cris LaPierre
le 7 Mar 2021
0 votes
See the documentation for fread (Read data from binary file). There are some examples on that page that should help you get started.
For an alternate approach using textscan, see this answer:
2 commentaires
chenna kesavulu ganna
le 8 Mar 2021
Cris LaPierre
le 8 Mar 2021
The extension doesn't really matter. What's more important is the actual formatting inside the file.
if ispc()
filename = 'myfile.raw';
S = fileread(filename);
else
%get the data into MATLAB as we are not given a sample file
S = {'00000 00 12 06 25 32 45 02 35 C2 00 B7 00 FD 00 4C 00'
'00010 63 00 B5 00 21 00 A3 00 61 00 D7 00 5A 00 25 00'
'00020 31 41 59 26 59 35'}; %illustrating ending short
S = strjoin(S, '\n');
end
S = regexprep(S, '^[0-9a-fA-F]{5}', '', 'lineanchors')
bytes = uint8(sscanf(S, '%2x', [1 inf]))
I changed the approach I was going to post involving textscan() in order to instead use a method that works even if the final line is not an exact multiple of 16 bytes. The textscan() approach I was going to use was fine at reading the data, but it padded the unused spaces on the line with 00, and even if I had changed that to a different constant there would have been the potential for problems. Besides, this approach works perfectly well.
8 commentaires
chenna kesavulu ganna
le 16 Mar 2021
Walter Roberson
le 16 Mar 2021
filename = 'myfile.raw';
outfile = 'myfile.csv';
perrow = 25;
S = fileread(filename);
S = regexprep(S, '^[0-9a-fA-F]{5}', '', 'lineanchors');
bytes = uint8(sscanf(S, '%2x', [1 inf]));
[fid, msg] = fopen(outfile, 'w');
if fid < 0
error('failed to open file "%s" because "%s"', outfile, msg);
end
fmt = [repmat('%d,', 1, perrow-1), '%d\n'];
fprintf(fid, fmt, bytes(1:end-1));
fprintf(fid, '%d\n', bytes(end));
fclose(fid);
This reads the .raw file, discarding the position information such as the 00020 . It converts the hexadecimal. Then it writes out the information to the given file outfile, writing perrow across.
The bit about about only writing to end-1 the first time has to do with ensuring that no comma is output after the last value.
chenna kesavulu ganna
le 17 Mar 2021
Walter Roberson
le 17 Mar 2021
Modifié(e) : Walter Roberson
le 17 Mar 2021
That error would happen if bytes was empty.
You have indicated to us that the file contains data such as
00000 00 12 06 25 32 45 02 35 C2 00 B7 00 FD 00 4C 00
which is a file that literally starts with '0' five times, then some spaces, then '0', '0', '', '1', '2' and so -- that everything you showed us is text in a file and the text happens to represent hexadecimal values that you want converted to decimal.
But now you show us something that has different colors and has fine ruler lines at the header and separating the parts. That cannot be a text file! The right side shows characters such as ü which are not part of same character set as the extended ascii line drawing characters. It is true that Unicode includes both https://en.m.wikipedia.org/wiki/Box-drawing_character but when you use the line drawing characters from Unicode in a text file, you cannot get them as thin as what is shown between lines in your new image.
You are not showing us the content of a file that happens to contain hexadecimal: you are showing us the graphical output of an application that is displaying a hexadecimal dump of something like an operating system or executable.
The difference is very important for this purpose, as it changes everything about the task. If your .raw file does not contain hexadecimal and instead contains binary data then nearly everything we gave you for code is irrelevant. So we need to know the actual file format to proceed.
chenna kesavulu ganna
le 17 Mar 2021
Walter Roberson
le 17 Mar 2021
Please attach a file containing (say) the first 4 lines of data. zip the file to attach it, as .raw files cannot be attached directly.
chenna kesavulu ganna
le 19 Mar 2021
Walter Roberson
le 19 Mar 2021
? MacroMedia RoboView files? That kind of .rdt ?
Catégories
En savoir plus sur Data Import and Export dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
