Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / March 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Alejandro Aleman - 13 Mar 2006 20:17 GMT
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
Marcus Heege - 13 Mar 2006 20:49 GMT
> Hello!
>
[quoted text clipped - 9 lines]
>
> 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
Jochen Kalmbach [MVP] - 13 Mar 2006 21:04 GMT
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
[quoted text clipped - 3 lines]
> 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::InteropServices::Marshal::StringToHGlobalAnsi(s).ToPointer()))
  {}
  ~StringConvA()
  {

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

struct StringConvW
{
  wchar_t *szUnicode;
  StringConvW(System::String* s)
    :
szUnicode(static_cast<wchar_t*>(System::Runtime::InteropServices::Marshal::StringToHGlobalUni(s).ToPointer()))
  {}
  ~StringConvW()
  {

System::Runtime::InteropServices::Marshal::FreeHGlobal(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>

Signature

Greetings
  Jochen

   My blog about Win32 and .NET
   http://blog.kalmbachnet.de/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.