Thread: System::String to Std::String

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    9

    System::String to Std::String

    I have a System.String object which holds a registry key value (a folder location)..

    I want to be able to convert it to Std.String.. When I do this:

    Code:
    std::cout << pValue->ToString() ;
    it just displays 1 instead of path..

    This works on the other hand:

    Code:
    Console::WriteLine(pValue);
    So I need a String value of pValue.. I can't figure it out!
    Last edited by smash84; 07-11-2006 at 03:48 PM.

  2. #2
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    EDIT: NVM
    My Website
    010000110010101100101011
    Add Color To Your Code!

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Managed C++ or C++/CLI? (VS.Net 2003 or 2005?)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    9
    Quote Originally Posted by CornedBee
    Managed C++ or C++/CLI? (VS.Net 2003 or 2005?)
    VS .NET 2003 sorry..

    I found a bunch of stuff on Google, but none of it compiles.. So I'm about all out of ideas

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You can get a std::wstring (using wide characters) out of a System.String pretty easily. You need to get the characters, pin them down, then construct a wstring from them.
    Code:
    System::String *netstr; // Fill with data.
    
    wchar_t characters __gc[] = netstr->ToCharArray();
    wchar_t __pin *pinner = &characters[0];
    std::wstring wstr(pinner, netstr->get_Length());
    Getting a std::string is trickier, because you also need to convert the unicode characters to a mbcs representation. You have two options.
    1) You can use the code above and then convert the characters in native code.
    2) You can convert the characters in managed code resulting in a byte array, and then use the technique above to get a char __pin* from the byte array and create a std::string from that. In that case, make sure you pass the length of the array as the length, not that of the string, as the string counts UTF-16 characters, which may differ from the number of bytes in whatever your local code page is. (Probably ISO-8859-1.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-14-2007, 03:34 PM
  2. std::string: Has my compiler gone nuts??
    By Andruu75 in forum C++ Programming
    Replies: 9
    Last Post: 09-28-2007, 04:02 AM
  3. Debugging help
    By cuddlez.ini in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2004, 07:08 PM
  4. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 12:34 PM
  5. returning std::string
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 09-24-2001, 08:31 PM