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++ / June 2004

Tip: Looking for answers? Try searching our database.

Conversion from/to String* to/from std::string

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Wild Wind - 08 Jun 2004 23:48 GMT
Hello,

I have the following methods I've written to convert
from String* to std::string and back:

static void ConvertFromStdString(std::string& inStr,
  System:String* outStr)
{
 outStr = new String(inStr.c_str());
}

static void ConvertToStdString(System::String* inStr,
  std::string& outStr)
{
 int len = inStr->Length();
 char* charStr = __nogc new char[len + 1];
 for (int i = 0; i < len; i++)
 {
  charStr[i] = (char)inStr->Chars[i];
 }
 charStr[len] = '\0';
 outStr = charStr;
}

Can anyone tell me if there might be some hidden errors
in these functions (perhaps due to a mixing of managed
and unmanaged variables)?

TIA,

Signature

Akin

aknak at aksoto dot idps dot co dot uk

Julie - 09 Jun 2004 00:17 GMT
> Hello,
>
[quoted text clipped - 23 lines]
> in these functions (perhaps due to a mixing of managed
> and unmanaged variables)?

The second fn will be pretty slow w/ the loop, and the cast to char is a
potential for somewhat undefined behavior for UNICODE characters (specifically,
chars above 8 bits).

You should take a look into Marshal::StringToHGlobalAnsi (and its counterpart
Marshal::FreeHGlobal) to get a copy of the string in one call, and assign that
to std::string.

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.