Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to convert String to char

0 views
Skip to first unread message

Kueishiong Tu

unread,
Sep 3, 2003, 8:29:17 AM9/3/03
to
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?

Nishant S

unread,
Sep 3, 2003, 8:39:25 AM9/3/03
to
String* str = S"Hello world";
char c = (char)str->Chars[0];

--
Regards,
Nish [VC++ MVP]

"Kueishiong Tu" <kst...@seed.net.tw> wrote in message
news:0b1101c37216$fdddbbc0$a301...@phx.gbl...

Jochen Kalmbach

unread,
Sep 3, 2003, 8:41:33 AM9/3/03
to
Kueishiong Tu wrote:

See: StringToHGlobalAnsi (System::Runtime::InteropServices::Marshal)
http://msdn.microsoft.com/library/en-
us/cpref/html/frlrfsystemruntimeinteropservicesmarshalclassstringtohglobala
nsitopic.asp

But conversion to ANSI is bad... because String is UNICODE... so if it
contains some unicode characters this might be lost.

Better convert to unicde: StringToHGlobalUni
or use PtrToStringChars (which is faster; if you do not need to modify the
string)

See also:
HOW TO: Convert from System::String* to Char* in Visual C++ .NET
http://support.microsoft.com/?kbid=311259

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/useritems/leakfinder.asp

Tomas Restrepo (MVP)

unread,
Sep 3, 2003, 8:20:41 PM9/3/03
to
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
tom...@mvps.org


Kueishiong Tu

unread,
Sep 4, 2003, 4:21:09 AM9/4/03
to
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

Jochen Kalmbach

unread,
Sep 4, 2003, 4:41:57 AM9/4/03
to
Kueishiong Tu wrote:

> 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.

As Tomas explained: You shouldn´t do that if you must handle other
characters than ANSI-characters.

Better Convert to w_chart or use some conversion routines:

<my post>


See: StringToHGlobalAnsi (System::Runtime::InteropServices::Marshal)
http://msdn.microsoft.com/library/en-
us/cpref/html/frlrfsystemruntimeinteropservicesmarshalclassstringtohgloba
la
nsitopic.asp

But conversion to ANSI is bad... because String is UNICODE... so if it
contains some unicode characters this might be lost.

Better convert to unicde: StringToHGlobalUni
or use PtrToStringChars (which is faster; if you do not need to modify
the
string)

See also:
HOW TO: Convert from System::String* to Char* in Visual C++ .NET
http://support.microsoft.com/?kbid=311259

</my post>

--
Greetings
Jochen

Do you need a memory-leak finder ?

http://www.codeproject.com/tools/leakfinder.asp

Nishant S

unread,
Sep 4, 2003, 6:38:14 AM9/4/03
to
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)" <tom...@mvps.org> wrote in message
news:#UnXjpnc...@TK2MSFTNGP11.phx.gbl...

0 new messages