필터 지우기
필터 지우기

Binary to floating point representation using IEEE-754

조회 수: 94 (최근 30일)
Arslan Ahmad
Arslan Ahmad 2018년 2월 22일
댓글: Anton Shishkin 2022년 9월 12일
I am given a Character array of length 32 made of only zeros and ones and i want to convert in floating point representation using IEEE-754. Can any one help???? My string is '10101110101101011010010110101001'. and my code uptill now is
sign=binary(1);
exp=binary(2:9);
mantissa=binary(10:32)
subt=bin2dec(exp);
e=-127+subt;
num=0;
for i=1:length(mantissa)
num=mantissa(i)*2^(-i)+num;
end
  댓글 수: 1
Arslan Ahmad
Arslan Ahmad 2018년 2월 22일
i did it at last
function [result] = mySingle2Decimal(binary)
sign=str2num(binary(1));
exp=binary(2:9);
mant=binary(10:32);
subt=bin2dec(exp);
e=-127+subt;
num=0;
for i=1:length(mant)
num=str2num(mant(i))*2^(-i)+num;
end
result=(-1)^sign*(1+num)*2^(e);

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

채택된 답변

Stephen23
Stephen23 2018년 2월 22일
편집: Stephen23 2018년 2월 22일
It is simple to avoid the loop:
S = '10101110101101011010010110101001';
%S = '00111110001000000000000000000000'; % Wikipedia example
V = S-'0'; % convert to numeric
frc = 1+sum(V(10:32).*2.^(-1:-1:-23))
pow = sum(V(2:9).*2.^(7:-1:0))-127
sgn = (-1)^V(1)
val = sgn * frc * 2^pow

추가 답변 (1개)

James Tursa
James Tursa 2018년 2월 22일
편집: James Tursa 2018년 2월 22일
Any method you choose is going to have to make assumptions about bit/byte ordering and the handling of the special inf, nan, and denormalized bit patterns. The following method simply assumes that the char array bit pattern you have matches the machine you are currently using and accounts for these special patterns:
result = typecast(uint32(bin2dec(S)),'single');
The other methods shown in this thread that work with the sign, exponent, and mantissa bits directly do not account for these special bit patterns.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by