binary to decimal when binary with radix point

1 次查看(过去 30 天)
I have to convert the following binary number to decimal: 11000101.101
I am using
bin2dec('binary number')
but is does not work with a radix point

回答(2 个)

Stephen23
Stephen23 2016-12-30
编辑:Stephen23 2016-12-30
This is easy to do yourself:
>> str = '11000101.101';
>> idx = str~='.';
>> vec = 1:sum(idx);
>> pwr = sum(str(idx)==str(vec)) - vec;
>> sum((str(idx)-'0') .* 2.^pwr)
ans =
197.625

Walter Roberson
Walter Roberson 2016-12-30
parts = strsplit(str, '.') ;
dec2bin(parts{1}) + dec2bin(parts{2})/2^length(parts{2})
  3 个评论
Stephen23
Stephen23 2016-12-30
@Harel Harel Shattenstein: there is a mistake in this untested code, the function dec2bin should actually be bin2dec. Otherwise it is correct.
Walter Roberson
Walter Roberson 2016-12-30
parts = strsplit(str, '.') ;
bin2dec(parts{1}) + bin2dec(parts{2})/2^length(parts{2})
That's what I get for posting at 3 in the morning ;-)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by