how to scan a hex file and then search for the required byte and display

8 Ansichten (letzte 30 Tage)
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.
  4 Kommentare
Prabhav
Prabhav am 3 Nov. 2013
The file is as follows.. can you please help me??
Prabhav
Prabhav am 3 Nov. 2013
ACKNOWLEDGEMENT:
Thanks for your suggestion to: @sixwwwwww

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

sixwwwwww
sixwwwwww am 1 Nov. 2013
Bearbeitet: sixwwwwww am 1 Nov. 2013
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 Kommentare
sixwwwwww
sixwwwwww am 3 Nov. 2013
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
Prabhav
Prabhav am 4 Nov. 2013
I have tried Increasing the memory size also but still the problem persists the code and the file is attached with it please help..
Thank you

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 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 Kommentare
Image Analyst
Image Analyst am 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)
Prabhav
Prabhav am 4 Nov. 2013
Bearbeitet: Prabhav am 4 Nov. 2013
Actually i meant the data set of 17 bytes that i'd need to evaluate will always start with A55A and should not have another A55A amongts the bytes, however the data set i'd need to plot is 5th and 6th byte within the 17 bytes if A5 is 1st byte and 5A is 2nd Byte. so the problem is to find the 5th and 6th byte in the 17 byte data after finding the 17 byte data starting from A55A
Thankyou
ACKNOWLEDGEMENT
sixwwwwww
Image Analyst

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Import and Export 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!

Translated by