How to convert String to char

K

Kueishiong Tu

How do I convert a managed String type to a native char
type? If nothing is availbale, how do I copy the the
contents of the String object to a char array?
 
T

Tomas Restrepo \(MVP\)

Hi Nishant,
String* str = S"Hello world";
char c = (char)str->Chars[0];

Well, that's one cast I'd stay away from, if you want your app to work when
"special" characters are in the string (remember that System::String is
UTF-16 encoded, while you are only handling 8-bit characters in char)
 
K

Kueishiong Tu

Dear Nish:
I tried it and it worked. As a matter of fact, I need to
convert the whole string. So I just put it in a loop.
Thank you very much.

Kueishiong Tu
 
N

Nishant S

Yup I know. But since he specifically wanted to cast to a char I guess we
can assume he wont be handling special characters.

--
Regards,
Nish [VC++ MVP]



Tomas Restrepo (MVP) said:
Hi Nishant,
String* str = S"Hello world";
char c = (char)str->Chars[0];

Well, that's one cast I'd stay away from, if you want your app to work when
"special" characters are in the string (remember that System::String is
UTF-16 encoded, while you are only handling 8-bit characters in char)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top