필터 지우기
필터 지우기

How do i convert base-2 number with decimal point into base-10?

조회 수: 6 (최근 30일)
Samson David Puthenpeedika
Samson David Puthenpeedika 2021년 10월 30일
답변: John D'Errico 2021년 10월 30일
Convert t base-2 numbers to base-10:
a. 110.01001
i tried using syntax bin2dec but it was giving error:
a='110.01001'
a = '110.01001'
bin2dec(a)
Error using bin2dec
Binary text must consist of characters 0 and 1.

답변 (2개)

Walter Roberson
Walter Roberson 2021년 10월 30일
split the character vector at the '.' and dec2bin() the two parts independently. Now divide the second one by 2^(number of digits), and add the result to the first converted value.

John D'Errico
John D'Errico 2021년 10월 30일
BIN2DEC does NOT support numbers with a decimal point in them. You can want it to happen, but it won't. Wanting code to behave as it was not designed to do is often a waste of time and want.
However, if you delete the decimal point, you will implicitly convert the number into an integer. Then you could remember where the decimal point fell, and divide by an appropriate power of 2. For example:
a = '110.01001';
decloc = find(a == '.')
decloc = 4
b = a; b(decloc) = '';
X = bin2dec(b)/2^(numel(a) - decloc)
X = 6.2813

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by