Clear Filters
Clear Filters

Binary String to ASCII

22 views (last 30 days)
Jared
Jared on 23 Oct 2012
If I have a character array of binary numbers, representing the ASCII code of a text string, how do I convert those numbers back to the text representation? The character array is 35x1, which should result in 5 ascii characters. I tried:
ascii_msg_decoded=char(bin2dec(reshape(bin_msg,[],7)));
It does convert to ASCII, but the characters are not correct.

Accepted Answer

Matt Fig
Matt Fig on 23 Oct 2012
Edited: Matt Fig on 23 Oct 2012
How did you do it? This seems to work.
M = 'Hello';
% Now encode:
Mbinlong = reshape(dec2bin(double(M),7).',[],1)
% Now decode:
mess = char(bin2dec(reshape(Mbinlong,7,[]).').')
  3 Comments
Jared
Jared on 23 Oct 2012
I should also add, that this received bit stream will not be stored as a character array like Mbinlong is above. It will be, in the case of this example, a 1x35 cell. This code will work still, with the intermediate step of converting that 1x35 cell into the character array that looks like Mbinlong. How would I do that?
Matt Fig
Matt Fig on 23 Oct 2012
Does each cell of the cell array have a message in it? If so, just access each cell one at a time.
M = {'Hello','Goodbye'};
% Now encode each message:
Mb = cellfun(@(x)reshape(dec2bin(x,7).',[],1),M,'Un',0);
% Now decode each message:
mess = cellfun(@(x)char(bin2dec(reshape(x,7,[]).').'),Mb,'Un',0)

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!