binary process, biner to decimal

조회 수: 7 (최근 30일)
Internazionale
Internazionale 2013년 3월 21일
i have binary number 20 bit (uint8), i want to convert the binary number to decimal. i use bin2dec but before i must convert num2str so i can use bin2dec. 20 bit (1x20) --> num2str --> 1x58 char. when i start to dec2bin, matlab error because binary string must be 52 bits or less. how to overcome this problem ?
  댓글 수: 1
Jan
Jan 2013년 3월 21일
The text description of the input is in general less exact as a shart Matlab example. What exactly is "a binary number 20 bit (uint8)? Is this a [1 x 20] uint8 vector, or a [1 x 3] uint8 vector with 4 unused bits?

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2013년 3월 21일
b = uint8([1 1 0 0 1 1 0 1 0 1 1 1 0 0 1 1 0 1 0 1]);
bin2dec( char(b + '0') )
  댓글 수: 2
Internazionale
Internazionale 2013년 3월 21일
it works, can you describe to me how it works ? especially when bin2dec (char(b+'0')), thanks
Walter Roberson
Walter Roberson 2013년 3월 22일
'0' is the character representation of zero. add 0 (the number) to it and you will have a value that is still the value for the character representation of '0'. Use char() to turn the value back into the character. So char(0 + '0') is going to be '0'.
Now, the character representation of '1' has a value that is exactly 1 greater than the value for the character representation '0'. ('1' - '0') is 1. So char(1 + '0') = '1'.
Therefore to turn 0 or 1 into '0' or '1', add '0' to the value and take char() of the result.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
Jan 2013년 3월 21일
편집: Jan 2013년 3월 21일
Guessing that your input is e.g.:
b = uint8([1 1 0 0 1 1 0 1 0 1 1 1 0 0 1 1 0 1 0 1]);
Then assuming, that the 1st element is the most significant bit:
d = 2 .^ (length(b)-1:-1:0) * double(b(:));
This is the dot-product of an [1xN] and [Nx1] vector, such that the sum is calculated implicitly.
  댓글 수: 1
Internazionale
Internazionale 2013년 3월 21일
and the output decimal ?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by