convert an array of 0s and 1s to binary & reverse

조회 수: 30 (최근 30일)
Houssam
Houssam 2021년 6월 13일
편집: Walter Roberson 2021년 6월 14일
hi Community,
I have an array of type double that contain only zeros and ones.
i want to convert it to binary value like below
[0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1] ----> [0 255]
and do the same thing in reverse
already tried de2bi()
but nothing !!!
  댓글 수: 2
Stephen23
Stephen23 2021년 6월 13일
편집: Stephen23 2021년 6월 14일
What shape is the input data? What encoding does it use?
Rik
Rik 2021년 6월 13일
@Stephen, isn't that the normal encoding? 0-255 is a normal 8 bit range, right? Or am I confused?

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

채택된 답변

Walter Roberson
Walter Roberson 2021년 6월 14일
편집: Walter Roberson 2021년 6월 14일
pad with 0 to a multiple of 8
2.^(7:-1:0) * reshape(padded, 8,[])
The result will be a vector of values 0 to 255
reverse:
bits = reshape(de2bi(bytes, 8),1,[])
and remember to remove the padding bits

추가 답변 (1개)

Stephen23
Stephen23 2021년 6월 14일
B = [0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1];
D = pow2(7:-1:0)*reshape(B,8,[])
D = 1×2
0 255

카테고리

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