Azzera filtri
Azzera filtri

Binary output formatting

24 visualizzazioni (ultimi 30 giorni)
Nil
Nil il 14 Mar 2012
Hi I have following part of program in my code which gives output as below but i want that output in below format
ciphertxt='nilesh';
disp(ciphertxt);
b1=dec2bin(ciphertxt,8)
disp(b1)
l=length(ciphertxt);
for i=1:l
t=ciphertxt(i);
n=abs(t);
b1=dec2bin(n,8);
disp(b1);
end
output
nilesh
01101110
01101001
01101100
01100101
01110011
01101000
I have tried with celldata=reshape(b1,1,[])assuming i will get everything in one row but not getting as excepted..please provide me direction
Desired output-
Required as string
'01101110 01101001 01101100 01100101 01110011 01101000'

Risposta accettata

Jacob Halbrooks
Jacob Halbrooks il 14 Mar 2012
Instead of displaying each piece of the string in the loop, you could append it onto a variable that is displayed once at the end:
ciphertxt='nilesh';
disp(ciphertxt);
l=length(ciphertxt);
strOutput = '';
for i=1:l
t=ciphertxt(i);
n=abs(t);
b1=dec2bin(n,8);
strOutput = [strOutput ' ' b1];
% disp(b1);
end
disp(strOutput);
If you need different formatting of the string, use SPRINTF.

Più risposte (0)

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Tag

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by