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

24 ビュー (過去 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 Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by