How to convert array of 1's and 0's to ascii character?

7 visualizaciones (últimos 30 días)
Flyers28
Flyers28 el 1 de Oct. de 2019
Comentada: Anfal D el 27 de Nov. de 2019
I have an array which at the end of my loop will contain a sequence of 1's and 0's which will contain a hidden message when decoded. I need to find away to convert this array into it's equivalent ascii characters for each segment of 8 bits. I generate this array of 1's and 0's by generating a logical array which I then convert to an integer array (probably unecessary if there is a more direct way). I then tried using dec2bin but I don't know what that does for me. So for example my integer array looks like:
Decode=[01000001] (type int8)
dec2bin - creates a column array of Decode in type char (this needs to be a row vector though).
So in this example the first 8-bits pertain to the decimal value 65 which is ASCII 'A'. At the end of my loop 'Decode' will be a long vector array containing many 8 bit sequences where each 8-bits corresponds to a particular ASCII character which will create a message at the end.
I can not figure out how to get Decode to ASCII.
  4 comentarios
James Tursa
James Tursa el 1 de Oct. de 2019
Editada: James Tursa el 1 de Oct. de 2019
Please state precisely what you are starting with, then we can suggest the most efficient way to get your final answer, probably without the need of looping. E.g., "Decode is a 50x8 uint8 array of 0's and 1's and I want each row of bit values combined and converted into the equivalent ASCII character". Then give a short example of Decode and the final answer you want from this short example.
Guillaume
Guillaume el 1 de Oct. de 2019
Editada: Guillaume el 1 de Oct. de 2019
Also specify if the character set is really ASCII (character codes between 0 and 127 only) or some other character set ,e.g. some 8-bit character set like ISO 8859-1 (latin 1), or a variable length encoding like UTF8.
Note that matlab characters are UTF16.

Iniciar sesión para comentar.

Respuesta aceptada

David Hill
David Hill el 1 de Oct. de 2019
bin2dec works off a character array ('10010011'). Convert 1 and 0 array to character array, and remove spaces.
a=num2str(Decode);
a=a(a~=' ');
output=[];
for i=1:9:length(a) %assuming you know that your binary stream is factor of 8 bits long
output=[output,char(bin2dec(a(i:i+7)))];
end
If you don't need to originally convert to a binary stream, I would stay in a uint8 array (easier to work with).
  1 comentario
Stephen23
Stephen23 el 2 de Oct. de 2019
Note that the output should be preallocated before the loop:
The loop is not necessary: as this answer notes, bin2dec already works on character arrays.

Iniciar sesión para comentar.

Más respuestas (1)

Flyers28
Flyers28 el 2 de Oct. de 2019
I ended up getting it to work. I was way over complicating it. I appreciate everyones response. Your way is way more efficient however. I have more lines of code then I needed for sure. If this was a way more complicated input I'd probaby bog down my computer with the ineffeciences but for what I needed it to do it worked. I'll see if I can play around with it using your structure during my free time as a learning experiment.
Thanks again everybody
  1 comentario
Anfal D
Anfal D el 27 de Nov. de 2019
Dear Flyers28;
i am working on hiding text message in frames
I recntally have issue on decode (extract) from the char array(contain 0's and 1's) an Ascii letters.
I am trying the answers here but it doesnot work.
can you help me please,
iam using version 2019b

Iniciar sesión para comentar.

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!

Translated by