how to scan a hex file and then search for the required byte and display
8 次查看(过去 30 天)
显示 更早的评论
in the file A5 5A should occur after 17 bytes so how to search it and conform it occurs only after 17 bytes then display the 5th and 6th byte starting from A5 and 5A. Could you please help me? , I have read your answer similar to it but couldnot manipulate the program accordingly.
采纳的回答
sixwwwwww
2013-11-1
编辑:sixwwwwww
2013-11-1
Dear Prabhav, you can do it as follows:
% Read file in the form of string
ID = fopen('filename.txt');
A = textscan(ID, '%s');
fclose(ID);
% Your array
A = A{:};
% Find the locations of bytes 'A5' within array
indA5 = find(ismember(A, 'A5'));
% Find the locations of bytes '5A' within array
ind5A = find(ismember(A, '5A'));
% Find whether all 'A5' and '5A' located consecutively
loc = all(ind5A - indA5 == 1);
% Find the difference in locations of two consectuve occurances of 'A5' and
% if the difference is 17 then stores 1 in variable 'difference' otherwise
% store '0'
difference = indA5(2:numel(indA5)) - indA5(1:numel(indA5) - 1) == 17;
I hope it is helpful. Good luck!
22 个评论
sixwwwwww
2013-11-3
here the problem is with the buffer size. So you can increase the buffer size as follows:
data = textscan(ID, '%s', 'BufSize', 10000);
This error is not because of code but because of buffer size(simply memory size) in your computer. You can try to increase buffer size as I described above
更多回答(1 个)
Image Analyst
2013-11-3
I know you accepted sixwwwwwwwwww's answer so I guess you have it solved already but it's not the way I would have done it at all. Way too complicated. I'd just read in the bytes with fread(), and simply extract the bytes you need with simple indexing, like this:
folder = 'C:\Users\Prabhav\Documents\Temporary';
fullFileName = fullfile(folder, 'hex.txt');
fid = fopen(fullFileName, 'rt');
numberOfBytesToRead = 2*(17+2) + 2*6
chars = fread(fid, numberOfBytesToRead, 'char')
fclose(fid);
% Extract 17th and 18th hex numbers
chars17and18 = chars(35:38)
fprintf('%c%c%c%c\n', chars17and18)
% Let's see it as characters in the command window:
char(chars17and18) % should show A55A if file is good.
% Now he wants to "display the 5th and 6th byte starting from A5 and 5A."
% This is not really clear if it's from the start of the A55A,
% or starting from the first byte after that, but I'll assume he wants the
% 23 and 24th bytes.
chars23and24 = chars(47:50)
fprintf('%c%c%c%c\n', chars23and24)
% Let's see it as characters in the command window:
char(chars23and24)
I know it looks longer and more complicated but that's just because there are comments and lines to display the bytes for you to look at them. The real main code is just the fread() line.
7 个评论
Image Analyst
2013-11-3
Not quite sure what you mean. I thought it was right after 17 hex numbers, so it would be the 18th and 19th number, if it were there, though if it's not there the 18th and 19th number would be something else. Of course you can just modify my code to look anywhere for the A5 using find().
folder = 'C:\Users\prabhav\Documents\Temporary';
fullFileName = fullfile(folder, 'hex.txt');
fid = fopen(fullFileName, 'rt');
% Define the max location where you would expect to find it, it it were there.
numberOfBytesToRead = 500;
chars = fread(fid, [1, numberOfBytesToRead], 'char');
fclose(fid);
% Find A55A (occurs in multiple places in this file).
lookingFor = 'A55A' - 0;
startingLocations = strfind(chars, lookingFor)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 String Parsing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!