Write a script that will input a number in binary code eg 11
Write a script that will input a number in binary code (eg., 1100-no blanks between the digits) and write its decimal value (12 in this case). Write a function right fill(s, n) that fills the string s with blanks from the right until its total length is n characters. If s is longer than n characters, it must be truncated.
Solution
This function will be used in program to convert the binary cde to decimal code. The full function is used:
function dec = bin2dec2(bin)
binstring = [];
if length(bin) > 1
for i = 1:length(bin)
tempstring = num2str(bin(i));
binstring = [binstring,tempstring];
end
else
binstring=num2str(bin);
end
for i = 1:length(binstring);
value(i)=str2double(binstring(i)) ...
*(2^(length(binstring)-i));
end
dec=sum(value);
