C++. Net convert System:: String to std:: string

Jak przekonwertować System::String na std:: string w C++. Net?

Author: Joel Coehoorn, 2009-08-19

6 answers

Istnieje czystsza składnia, jeśli używasz najnowszej wersji. Net

#include "stdafx.h"
#include <string>

#include <msclr\marshal_cppstd.h>

using namespace System;

int main(array<System::String ^> ^args)
{
    System::String^ managedString = "test";

    msclr::interop::marshal_context context;
    std::string standardString = context.marshal_as<std::string>(managedString);

    return 0;
}

To również daje lepsze sprzątanie w obliczu WYJĄTKÓW.

Istnieje artykuł msdn dla różnych innych konwersji

 60
Author: Colin Gravill,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-08-21 08:47:05

I w odpowiedzi na" łatwiejszy sposób " w późniejszych wersjach C++/CLI, można to zrobić bez marshal_context. Wiem, że to działa w Visual Studio 2010; nie jestem pewien co do tego wcześniej.


#include "stdafx.h"
#include <string>

#include <msclr\marshal_cppstd.h>

using namespace msclr::interop;

int main(array<System::String ^> ^args)
{
    System::String^ managedString = "test";

    std::string standardString = marshal_as<std::string>(managedString);

    return 0;
}

 27
Author: Mike Johnson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-04-23 18:07:45
stdString = toss(systemString);

  static std::string toss( System::String ^ s )
  {
    // convert .NET System::String to std::string
    const char* cstr = (const char*) (Marshal::StringToHGlobalAnsi(s)).ToPointer();
    std::string sstr = cstr;
    Marshal::FreeHGlobal(System::IntPtr((void*)cstr));
    return sstr;
  }
 7
Author: Spencer Ruport,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-08-19 15:40:39

C # używa formatu UTF16 dla swoich ciągów.
Tak więc, oprócz tylko konwersji typów, powinieneś również być świadomy o rzeczywistym formacie ciągu.

Podczas kompilacji dla wielobajtowego zestawu znaków Visual Studio i Win API przyjmują UTF8(w rzeczywistości kodowanie windows, które jest Windows-28591).
Podczas kompilacji dla zestawu znaków Unicode Visual studio i Win API przyjmują UTF16.

Więc musisz przekonwertować ciąg znaków z UTF16 do formatu UTF8 jak również, i nie tylko konwertować do std:: string.
Będzie to konieczne podczas pracy z formatami wieloznakowymi, takimi jak niektóre języki inne niż łacińskie.

Chodzi o to, aby zdecydować, że std::wstring zawsze reprezentuje UTF16 .
Oraz std::string zawsze reprezentuje UTF8 .

To nie jest wymuszane przez kompilator, to bardziej dobra polityka.

#include "stdafx.h"
#include <string>

#include <msclr\marshal_cppstd.h>

using namespace System;

int main(array<System::String ^> ^args)
{
    System::String^ managedString = "test";

    msclr::interop::marshal_context context;

    //Actual format is UTF16, so represent as wstring
    std::wstring utf16NativeString = context.marshal_as<std::wstring>(managedString); 

    //C++11 format converter
    std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> convert;

    //convert to UTF8 and std::string
    std::string utf8NativeString = convert.to_bytes(utf16NativeString);

    return 0;
}

Lub mieć ją w bardziej zwartej składni:

int main(array<System::String ^> ^args)
{
    System::String^ managedString = "test";

    msclr::interop::marshal_context context;
    std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> convert;

    std::string utf8NativeString = convert.to_bytes(context.marshal_as<std::wstring>(managedString));

    return 0;
}
 6
Author: Yochai Timmer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-25 09:42:32

Miałem zbyt wiele niejasnych błędów pojawiających się z powyższymi odpowiedziami (tak, jestem noobem C++)

To mi pomogło przy wysyłaniu ciągów z C# do C++ CLI

C #

bool result;
result = mps.Import(mpsToolName);

C++ CLI

Funkcja:

bool ManagedMPS::Import(System::String^ mpsToolNameTest)
std::string mpsToolName;
mpsToolName = toStandardString(mpsToolNameTest);

Funkcja, która działa od konwersji String^ do std:: string

static std::string toStandardString(System::String^ string)
{
 using System::Runtime::InteropServices::Marshal;
 System::IntPtr pointer = Marshal::StringToHGlobalAnsi(string);
 char* charPointer = reinterpret_cast<char*>(pointer.ToPointer());
 std::string returnString(charPointer, string->Length);
 Marshal::FreeHGlobal(pointer);
 return returnString;
}
Po dalszych badaniach wydaje się, że jest to czystsze i bezpieczniejsze.

Przełączyłem się na użycie tej metody.

std::string Utils::ToUnmanagedString(String^ stringIncoming)
{
   std::string unmanagedString = marshal_as<std::string>(stringIncoming);
   return unmanagedString;
}
 4
Author: Tom Stickel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-24 01:31:13

Tworzenie komponentu Windows Runtime, którego możesz użyć:

String^ systemString = "Hello";
std::wstring ws1(systemString ->Data());
std::string standardString(ws1.begin(), ws1.end());
 0
Author: Flayn,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-15 11:50:15