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!