how to scan a hex file and then search for the required byte and display
Show older comments
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.
Accepted Answer
More Answers (1)
Image Analyst
on 3 Nov 2013
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 Comments
Image Analyst
on 3 Nov 2013
Each hex number is 2 bytes, for example A5 - that's an "A" which is one byte, and a "5" which is another byte. So 17 hex numbers would be 2*17 so we need to skip past those. Then we need to take the 2 numbers starting after the 17th. So that's how I get 2*(17+2). Then you need to take 6 extra numbers because you need to get the 5th and 6th number so I add on 2*6 bytes to make sure I read those in. No messing with cell arrays or for loops or anything - just a simple and clean call to fread.
Dear Image Analyst, I think each hex number is equivalent to 4 bit(half byte) and thus A5 represents 1 byte. See for example: http://en.wikipedia.org/wiki/Hexadecimal. Because hex has base 16 = 2^4 so only 4 bits are required for one hex number and so two hex numbers make one byte.
Image Analyst
on 3 Nov 2013
Yes, but he has a text file, not a binary file. What you say would be true if it were a binary file, but it's not - it's a text file. So the nibbles are actually taking up one whole ASCII character, 8 bits, one byte. So a hex number like A5 takes up two ASCII characters or two bytes, not one.
Image Analyst
on 3 Nov 2013
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)
Categories
Find more on Data Import and Export in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!