Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reading Hex data to text.

10 views
Skip to first unread message

Adrian

unread,
Oct 6, 2010, 2:29:04 AM10/6/10
to
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

Janis Doebler

unread,
Oct 7, 2010, 8:16:03 AM10/7/10
to
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 new messages