469,950 Members | 1,902 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Conversion from System::String::^ to char*

Hello!

i know this may be a newbie question, but i need to convert a string from
System::String^ to char*, in the msdn page tells how, but i need to set to
/clr:oldSyntax and i dont want it because in further editions of .net this
will be deprecated or so on.. .

so, please, anybody can tellme how to convert from System::String::^ to
char* and viceversa?

thank you very much!

alejandro aleman
Mar 13 '06 #1
2 4671
"Alejandro Aleman" <al**************@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hello!

i know this may be a newbie question, but i need to convert a string
from System::String^ to char*, in the msdn page tells how, but i need to
set to /clr:oldSyntax and i dont want it because in further editions of
.net this will be deprecated or so on.. .

so, please, anybody can tellme how to convert from System::String::^ to
char* and viceversa?

thank you very much!

alejandro aleman


To convert from . . to . . use .
[const] char* System::String^ String(char*)
constructor
[const] wchar_t* System::String^ String(wchar_t*)
constructor
BSTR System::String^ String(wchar_t*)
constructor
System::String^ [const] char*
Marshal::StringToCoTaskMemAnsi or Marshal::StringToHGlobalAnsi
System::String^ wchar_t*
Marshal::StringToCoTaskMemUni or Marshal::StringToHGlobalUni
System::String^ const wchar_t*
PtrToStringChars(String^)
System::String^ BSTR
Marshal::StringToBSTR

Marcus Heege
Mar 13 '06 #2
Hi Alejandro!
i know this may be a newbie question, but i need to convert a string from
System::String^ to char*, in the msdn page tells how, but i need to set to
/clr:oldSyntax and i dont want it because in further editions of .net this
will be deprecated or so on.. .

so, please, anybody can tellme how to convert from System::String::^ to
char* and viceversa?


For convertion char*/wchar_t* into System::String you need just the
"constructor of "System::String".

For the conversion to char*/wchar_t* you need to explicit allocate
memory and therefor I suggest to use the following method.
It seems to be overloaded, but it can handle all situations (like
exceptions and so on...):

<code>
struct StringConvA
{
char *szAnsi;
StringConvA(System::String* s)
:
szAnsi(static_cast<char*>(System::Runtime::Interop Services::Marshal::StringToHGlobalAnsi(s).ToPointe r()))
{}
~StringConvA()
{

System::Runtime::InteropServices::Marshal::FreeHGl obal(IntPtr(szAnsi));
}
operator LPCSTR() const
{
return szAnsi;
}
};

struct StringConvW
{
wchar_t *szUnicode;
StringConvW(System::String* s)
:
szUnicode(static_cast<wchar_t*>(System::Runtime::I nteropServices::Marshal::StringToHGlobalUni(s).ToP ointer()))
{}
~StringConvW()
{

System::Runtime::InteropServices::Marshal::FreeHGl obal(IntPtr(szUnicode));
}
operator LPCWSTR() const
{
return szUnicode;
}
};

#ifdef _UNICODE
#define StringConvT StringConvW
#else
#define StringConvT StringConvA
#endif
int _tmain()
{
String *s = S"abc";
std::string ansi = StringConvA(s);
std::wstring unicode = StringConvW(s);
_tprintf(_T("%s"), (LPCTSTR) StringConvT(s));
}
</code>
--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Mar 13 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by LadyEve | last post: by
15 posts views Thread by Yifan | last post: by
7 posts views Thread by Jonathan Porter | last post: by
24 posts views Thread by Marcus Kwok | last post: by
14 posts views Thread by =?Utf-8?B?Sm9hY2hpbQ==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.