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

 Respuesta aceptada

Thorsten
Thorsten el 28 de Oct. de 2015
s = char(bin+'0')

3 comentarios

azade
azade el 28 de Oct. de 2015
can you explain that ,please? I want to get 'h' in output.
Thorsten
Thorsten el 28 de Oct. de 2015
Editada: Thorsten el 28 de Oct. de 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 el 28 de Oct. de 2015
that's what I wanted. Thank you

Iniciar sesión para comentar.

Más respuestas (1)

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

Categorías

Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 28 de Oct. de 2015

Comentada:

el 28 de Oct. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by