Reading Hex data to text.

  • Follow


I am fairly new to matlab, and am just having some trouble reading in part of a data set from hex to text. 

A sample of the data would be : 
36 00 00 00 73 61 6d 70 6c 65 00
Where the first byte is the length of the string that appears after the three empty(00) bytes. 
In a regular hex editor i can read the string as askii, but I have not been able to find a function in Matlab that will allow me to do this. I've tried Text scan, fread and fgetl to no avail. 

All i'm trying to do is read in the text with each value on a new line. I know this format is used in a few cases, i am just out of ideas on how to read it into matlab. 

Thanks, 
Adrian
0
Reply Adrian 10/6/2010 6:29:04 AM

Is there a carrier return in the textfile? If yes, then you can read each line and convert it.
Otherwise you can also read the first X bytes and work on it, too, using fread

For your example it might be better to read the first byte, then you know the following size, and read the next bytes.

For converting hex to char: char(hex2num(str)).

---
% reading
fid = fopen(filename,'r');
firstByte = fread(fid,1,'*char');
length = hex2num(str);
data = fread(fid,length,*char);
fclose(fid)

% converting (quick 'n' dirty)
str(length) = ' '; % Prelocate
for k = 1 : length;
 string(k) = char(hex2num(data(2*k-1:2*k)) ; 
end;

---
I did not test it, just typed it by mind.
0
Reply Janis 10/7/2010 12:16:03 PM


1 Replies
548 Views

(page loaded in 0.028 seconds)

Similiar Articles:













7/21/2012 5:41:47 PM


Reply: