Converting binary to decimal and vice versa, and converting decimal to IEEE-754 format. I have to use if-elseif and for/while loop construct

3 views (last 30 days)
I have no idea where to start. I have to create three different codes that will do each of those functions using if-elseif and for/while constructs.
  1 Comment
Rayan Hussaini
Rayan Hussaini on 1 Nov 2015
Edited: Walter Roberson on 1 Nov 2015
I have binary to decimal. Now I just need the other two
function decimal= mybin2real(binarystring)
% Converts an input string of 1s and 0s to a number.
Ind = strfind(binarystring, '.');
L = length(binarystring);
if isempty(Ind)
Ind = L+1;
end
Num1 = binarystring(1:Ind-1);
LN1 = length(Num1);
Num2 = binarystring(Ind+1:end);
LN2 = length(Num1);
dec1=0;
for ii = 1 : LN1
dec1 = dec1 + str2double(Num1(LN1-ii+1)) * 2^(ii-1);
end
dec2=0;
for ii = 1 : length(Num2)
dec2 = dec2 + str2double(Num2(ii)) * 2^-(ii);
end
decimal=dec1+dec2;

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!