Read .dat file - binary data

조회 수: 3 (최근 30일)
Luca Finazzi
Luca Finazzi 2017년 4월 24일
댓글: Nikhil Sreekumar 2017년 4월 27일
Dear all,
I'm getting trouble reading a dat file containing binary datas. The .dat files look like this:
0000110001000001
0001100001100100
0010010001001111
0010111111100100
0011101100001000
The datas are 16 bit 2'complement signed fractionary (fract part is 15 bits). I'm not able to find a way to get a right interpretation of the data. Is there a way to to that?
Thanks a lot,
Luca.
  댓글 수: 2
Luca Finazzi
Luca Finazzi 2017년 4월 25일
I found a way to solve the issue. Probably it isn't the best way to do that, but I post the code, hope will be helpfull
function [out] = fix2dec(input, word_len, fract_len, sign);
% convert a string rapresentation of datas into a vector of decimal values
% input : string
% word_len, fract_len : integer
% sign : boolean (1 signed, 0 unsigned)
%range controls
if ( sign ~= 0 && sign ~= 1 )
error('Sign out of range: 1 signed 0 unsigned');
end
M = zeros(ceil(length(input)/(word_len+2)),word_len+2);
out = zeros(1,ceil(length(input)/(word_len+2)));
for k = 1:1:ceil(length(input)/(word_len+2))-1
for n = 1:1:word_len+2
M(k,n) = input(n+((word_len+2)*(k-1)));
if (n<word_len+1 && n>1)
out(k) = out(k)+(M(k,n)-'0')*2^(word_len-fract_len-n);
end
end
if(sign == 1 && M(k,1) == '1')
out(k) = -2^(word_len-1-fract_len)+out(k);
end
end
end
Nikhil Sreekumar
Nikhil Sreekumar 2017년 4월 27일
Hi Luca,
This file exchange submission might help you with the issue.
Thanks
Nikhil

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by