convert an array of 0s and 1s to binary & reverse
30 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Houssam
el 13 de Jun. de 2021
Editada: Walter Roberson
el 14 de Jun. de 2021
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 comentarios
Rik
el 13 de Jun. de 2021
@Stephen, isn't that the normal encoding? 0-255 is a normal 8 bit range, right? Or am I confused?
Respuesta aceptada
Walter Roberson
el 14 de Jun. de 2021
Editada: Walter Roberson
el 14 de Jun. de 2021
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
0 comentarios
Más respuestas (1)
Stephen23
el 14 de Jun. de 2021
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,[])
0 comentarios
Ver también
Categorías
Más información sobre Data Type Conversion en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!