Converting binary to decimal and vice versa, please help!!

I'm having trouble converting strings that represent binary numbers to decimal and vice versa. The main issue is that I'm unsure of how to deal with the decimal point, say converting -11.11 to decimal. This is a homework assignment, so I'm not allowed to use the built in functions.
Here's my code for one of the two:
function decimal= mybin2real(binarystring)
decimal = 0;
for i = 1 : length(binarystring)
decimal = decimal + str2num(binarystring(i)) * 2^(length(binarystring) - i);
end
decimal
end
I was thinking about finding where the decimal point was using strfind, but i'm not sure how to implement it in the matlab code.

6 件のコメント

Daniel Shub
Daniel Shub 2012 年 11 月 2 日
編集済み: Daniel Shub 2012 年 11 月 2 日
You realize that your example code is littered with built in functions. As bin2dec is not a builtin function, why not just use it. Or maybe retype the function yourself. Have you looked at how bin2dec solves the problem?
SB
SB 2012 年 11 月 2 日
My bad, I meant I can't use the bin2dec function.
Daniel Shub
Daniel Shub 2012 年 11 月 2 日
I will repeat myself: Have you looked at how bin2dec solves the problem. In general the code included in MATLAB is of pretty high quality.
SB
SB 2012 年 11 月 2 日
I tried to look at documentation, but didn't find much. Could you either supply me with where to look so I could understand the methodology, or could you look at my code below and help me through there?
Daniel Shub
Daniel Shub 2012 年 11 月 2 日
At the command line type
type bin2dec
or
edit bin2dec
this will let you see the source code.
SB
SB 2012 年 11 月 2 日
Thanks alot, that's really helpful, I didn't know we could even do that.

サインインしてコメントする。

 採用された回答

Matt Fig
Matt Fig 2012 年 11 月 2 日
編集済み: Matt Fig 2012 年 11 月 2 日

0 投票

Here I have simply fixed your code. You were really close!
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;

6 件のコメント

SB
SB 2012 年 11 月 2 日
The code returns NaN when I run it, additionally, I think I should have the output as well as the input as character strings.
Matt Fig
Matt Fig 2012 年 11 月 2 日
編集済み: Matt Fig 2012 年 11 月 2 日
What input did you give?
>> mybin2real('10101.101')
ans =
21.625
>> mybin2real('1010.101')
ans =
10.625
>> mybin2real('1010.101111')
ans =
10.734375
>> mybin2real('11010111.101111001')
ans =
215.736328125
SB
SB 2012 年 11 月 2 日
I don't think the code works for negative signs, since mybin2real('-11.11') returns NaN.
Matt Fig
Matt Fig 2012 年 11 月 2 日
Well that's an easy fix, yes? As the first step, simply check if there is a negative sign. If so, remove it and negate the number at the end.... No Big Deal....
SB
SB 2012 年 11 月 2 日
so like, if strcmp(binarystring(1),'-')==1 decimalvalue=-dec1-dec2
or something like that? I'm not quite sure, to be honest.
Matt Fig
Matt Fig 2012 年 11 月 2 日
No. As the very first thing....
if binarystring(1)=='-'
binarystring = binarystring(2:end);
F = -1;
else
F = 1;
end
Then at the end:
decimal = decimal*F;

サインインしてコメントする。

その他の回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2012 年 11 月 2 日
編集済み: Andrei Bobrov 2012 年 11 月 3 日

2 投票

decimal = (binarystring-'0')*pow2(size(binarystring,2)-1:-1:0).';
ADD
binarystring = '-11.11';
b = binarystring - '0';
[~,s] = ismember([-3,-2],b);
p = b(b >= 0);
decimal = sign(b(1))*p*pow2(diff(s) + (-2:-1:-1-numel(p)))';

2 件のコメント

SB
SB 2012 年 11 月 2 日
That doesn't work since something like mybin2real('-11.11') outputs -77 instead of -3.75 (it doesnt look for the decimal point)
Andrei Bobrov
Andrei Bobrov 2012 年 11 月 2 日
see ADD part

サインインしてコメントする。

prince kumar
prince kumar 2018 年 7 月 15 日
編集済み: KSSV 2019 年 10 月 23 日

0 投票

n=input('input binary no.=');
n=int2str(n); s=0;
for i=1:numel(n)
s=s+(str2num(n(i))*(2.^(numel(n)-i)));
end
s

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

質問済み:

SB
2012 年 11 月 2 日

編集済み:

2019 年 10 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by