I need to reproduce in C# a MATLAB code I found, which reads a binary file. The code is:
% Skip header
fread(fid, 1, 'int32=>double', 0, 'b');
% Read one property at the time
i = 0;
while ~feof(fid)
i = i + 1;
% Read field name (keyword) and array size
keyword = deblank(fread(fid, 8, 'uint8=>char')');
keyword = strrep(keyword, '+', '_');
num = fread(fid, 1, 'int32=>double', 0, 'b');
% Read and interpret data type
dtype = fread(fid, 4, 'uint8=>char')';
End
fclose(fid)
I’ve tried several methods of reading binary files in C#, but I haven’t got the right results. How should I proceed?
this is what i've done, that seems to kind of work so far
FileStream fs = new FileStream(filename, FileMode.Open);
BinaryReader binreader = new BinaryReader(fs,Encoding.Default);
//skip head
binreader.ReadInt32();
for (int i = 0; i < 8; i++)
{
keyword = keyword + binreader.ReadChar();
}
keyword = keyword.TrimEnd();
keyword = keyword.Replace("+", "_");
num = binreader.ReadInt32();
for (int i = 0; i < 4; i++)
{
dtype = dtype + binreader.ReadChar();
}
the problem is that i should obtein: keyword=INTERHEAD, num=411 and dtype=INTE but what Im getting is: keyword=INTERHEAD, num=-1694433280 and dtype=INTE the problem is in getting the num variable right.
I've changed readint32 to readdouble, readUint32 and so on but never got 411.
Any help?