binary to decimal when binary with radix point

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

Respuestas (2)

Stephen23
Stephen23 el 30 de Dic. de 2016
Editada: Stephen23 el 30 de Dic. de 2016
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 el 30 de Dic. de 2016
parts = strsplit(str, '.') ;
dec2bin(parts{1}) + dec2bin(parts{2})/2^length(parts{2})

3 comentarios

Harel Harel Shattenstein
Harel Harel Shattenstein el 30 de Dic. de 2016
Editada: Harel Harel Shattenstein el 30 de Dic. de 2016
I should use a command as + does not seems to be defined for those variables (parts{1} and parts{2} are strings)
Stephen23
Stephen23 el 30 de Dic. de 2016
@Harel Harel Shattenstein: there is a mistake in this untested code, the function dec2bin should actually be bin2dec. Otherwise it is correct.
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 ;-)

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 30 de Dic. de 2016

Comentada:

el 30 de Dic. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by