Binary String to ASCII

21 次查看(过去 30 天)
Jared
Jared 2012-10-23
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.

采纳的回答

Matt Fig
Matt Fig 2012-10-23
编辑:Matt Fig 2012-10-23
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 个评论
Jared
Jared 2012-10-23
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 2012-10-23
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)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by