convert string to bits and then convert bits back to strings ....

14 ビュー (過去 30 日間)
user06
user06 2015 年 5 月 1 日
コメント済み: Joseph Cheng 2015 年 5 月 7 日
facing problem with this code..
word='hello';
wordBinary = reshape(dec2bin(word,7)',1,[]);
% note the <'> after the dec2bin, to transpose the matrix
for j=1:size(wordBinary,2)
wordOutput(1,j) = str2double(wordBinary(1,j));
end
disp(wordOutput);
for j=1:size(wordOutput,2)
s(1,j) = num2str(wordOutput(1,j));
end
t = reshape(s, length(s)/7, 7);
decimalValues = bin2dec(t);
y=char(decimalValues);
disp(y);
output is coming wrong.. please help. o/p is:J U - { please help me in this..
  1 件のコメント
Jurgen
Jurgen 2015 年 5 月 1 日
編集済み: Jurgen 2015 年 5 月 1 日
I think dec2bin can not take a string...
Wordoutput is not declared before it is used.
I think reshape is not needed, because a string is 1xn sized by default.

サインインしてコメントする。

回答 (2 件)

Joseph Cheng
Joseph Cheng 2015 年 5 月 1 日
編集済み: Joseph Cheng 2015 年 5 月 1 日
the issue is with your
t =reshape(s,length(s)/7,7);
it should read
t = reshape(s,length(s)/length(word),length(word));
decimalValues = bin2dec(t');
if you look at your original reshape you can see that it is basically transposed. This is because reshape does not fill in rows first then columns. Reshape takes the array and starts filling in the reshaped matrix starting from column 1 then moves to column 2 and so on.
this short example shows what is happening
x = [1 2 3 4 5 6 7 8 9 10 11 12]
reshape(x,3,4)
  2 件のコメント
user06
user06 2015 年 5 月 7 日
if i am using what u said then is showing nothing as the output. it is saying that y is 7*1 char array but what are the values, i m not getting.
Joseph Cheng
Joseph Cheng 2015 年 5 月 7 日
really i would say you don't understand what is happening and you should step through it step by step looking at each line in debug mode. since
word='hello';
wordBinary = reshape(dec2bin(word,7)',1,[]);
% note the <'> after the dec2bin, to transpose the matrix
for j=1:size(wordBinary,2)
wordOutput(1,j) = str2double(wordBinary(1,j));
end
disp(wordOutput);
for j=1:size(wordOutput,2)
s(1,j) = num2str(wordOutput(1,j));
end
t = reshape(s,length(s)/length(word),length(word));
decimalValues = bin2dec(t');
y=char(decimalValues);
disp(y');
works just fine

サインインしてコメントする。


Guillaume
Guillaume 2015 年 5 月 1 日
編集済み: Guillaume 2015 年 5 月 1 日
I'm not really sure what output you're looking for, but I would do it like this:
word = 'hello';
wordbinary = dec2bin(word', 7) - '0'
originalword = char(bin2dec(char(wordbinary + '0')))'

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by