Convert the contents of array to char

hi everybody i want convert content of array to char. for example for 'h'
bin=[0,0,0,1,0,1,1,0];
>> char(bin2dec(num2str(bin)))
but, this does not work. can anyone help me? thanks

 Réponse acceptée

Thorsten
Thorsten le 28 Oct 2015
s = char(bin+'0')

3 commentaires

azade
azade le 28 Oct 2015
can you explain that ,please? I want to get 'h' in output.
Thorsten
Thorsten le 28 Oct 2015
Modifié(e) : Thorsten le 28 Oct 2015
If you add '0' (or double('0') = 48), you convert the numbers to their ASCII code of the digit, so 0 becomes 48 and 1 becomes 49. With char you convert the number to a characters.
But now I understand that this is not what you want.
To convert the bin to the corresponding char, use
char(bin2dec(fliplr(char(bin+'0'))))
Note that you have to convert the binary numbers in bin to binary string using char(bin+'0'), and you have to flip it, such that the least significant bit get to the right, before you can feed it into bin2dec.
If you don't want to use bin2dec, you can use
char(bin*[1 cumprod(2*ones(1,numel(bin)-1))]')
azade
azade le 28 Oct 2015
that's what I wanted. Thank you

Connectez-vous pour commenter.

Plus de réponses (1)

Jan
Jan le 28 Oct 2015
Or:
bin = [0,0,0,1,0,1,1,0];
pool = '01';
str = pool(bin + 1);

Catégories

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by