MATLAB: How to convert array of 1’s and 0’s to ascii character

array to asciiasciibin to ascii

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.

Best Answer

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).