469,358 Members | 1,626 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,358 developers. It's quick & easy.

How to convert String to char

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?
Nov 16 '05 #1
4 11131
String* str = S"Hello world";
char c = (char)str->Chars[0];

--
Regards,
Nish [VC++ MVP]

"Kueishiong Tu" <ks****@seed.net.tw> wrote in message
news:0b****************************@phx.gbl...
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?

Nov 16 '05 #2
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)

--
Tomas Restrepo
to****@mvps.org
Nov 16 '05 #3
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
Nov 16 '05 #4
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)" <to****@mvps.org> wrote in message
news:#U*************@TK2MSFTNGP11.phx.gbl...
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)

--
Tomas Restrepo
to****@mvps.org

Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by IamZadok | last post: by
7 posts views Thread by MilanB | last post: by
15 posts views Thread by Yifan | last post: by
12 posts views Thread by Brian Henry | last post: by
2 posts views Thread by Steve Kershaw | last post: by
5 posts views Thread by Zytan | last post: by
12 posts views Thread by Peter | last post: by
14 posts views Thread by rtillmore | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.