Single-byte characters?

Hi!
If I understand correctly, characters in Matlab are always defined as two-byte variables? Is it possible to define a single-byte character?
I'm trying to communicate with an external device, and I need to send it a character string of N bytes = N characters. At the moment, however, when I define my string in Matlab I always get 2*N bytes.
Is it possible to create strings of characters where each character is stored as one byte?
Thanks!

Respuestas (4)

Patrick Kalita
Patrick Kalita el 24 de Mayo de 2011

1 voto

Does this work for you?
>> s = uint8('hello world')
s =
104 101 108 108 111 32 119 111 114 108 100
>> whos
Name Size Bytes Class Attributes
s 1x11 11 uint8
>>

1 comentario

John D'Errico
John D'Errico el 24 de Mayo de 2011
And of course, char(s) recovers the characters themselves.

Iniciar sesión para comentar.

Jan
Jan el 24 de Mayo de 2011

1 voto

If the function expects a CHAR, you must provide 2-byte characters. 1-byte characters are simply not CHARs and will be refused by the called function.
Please post any details about the program used for "communication with an external device".
Lipa
Lipa el 24 de Mayo de 2011

0 votos

Thanks for the reply. I tried what you suggested before, but the problem is that my external function that I'm calling requiers a character string as input. I know that generally (single-byte) "char" should be the same as "uint8", but this doesn't seem to be the case in Matlab? When I'm calling with uint8, I get the error: "Parameter can not be converted to a string."
So it seems that the input must be of "char" type, yet only one byte should be reserved for each character in the string...

1 comentario

Patrick Kalita
Patrick Kalita el 24 de Mayo de 2011
Which function is giving you the error? Is it something provided by MATLAB or a Toolbox?

Iniciar sesión para comentar.

Laura Proctor
Laura Proctor el 24 de Mayo de 2011

0 votos

You can try the function unicode2native which will convert your characters into numeric bytes depending on the encoding scheme that you select:
bytes = unicode2native('hello world','UTF-8')
But I don't know that this will resolve the issue. Can you copy and paste the exact command you are using as well as the error message?

Categorías

Etiquetas

Preguntada:

el 24 de Mayo de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by