failed binary to decimal
显示 更早的评论
hello everyone, I have the code below , which I want to use for conversion of binary to decimal, but each time I run the program, the software keeps saying >> binary_conversion
Not enough input arguments.
Error in binary_conversion (line 2)
count=numberofdigits(x);
Please I need held on this.
Here is the code:
function decimal_value= binary_conversion(x)
count=numberofdigits(x);
pow=0;
decimal_value=0;
for i=1:1:count
n=mod(x,10);
decimal_value=decimal_value+(n*2^pow);
pow=pow+1;
x=floor(x/10);
end
end
回答(1 个)
How do you try to run the code? Using the green triangle in the editor? Then now input argument is used.
Call it from the command line or from another script or function with an input:
d = binary_conversion(10010101)
A simplification of your code:
function d = binary_conversion(x)
count = numberofdigits(x);
mult = 1;
d = 0;
for i = 1:count
d = d + mod(x, 10) * mult;
mult = mult * 2;
x = floor(x / 10);
end
end
类别
在 帮助中心 和 File Exchange 中查找有关 Serial Port (RS232) Protocol Blocks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!