- #include <string>
- #include <iostream>
- using namespace System;
- using namespace std;
- int main() {
- string str = "test";
- cout << str << endl;
- String^ str2 = gcnew String(str.c_str());
- Console::WriteLine(str2);
- // alternatively
- String^ str3 = gcnew String(str.c_str());
- Console::WriteLine(str3);
2.
#include <string> #include <iostream> using namespace std; using namespace System; using namespace System::Runtime::InteropServices; //System::String转std::string void MarshalString ( String* s, string& os ) { const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer(); os = chars; Marshal::FreeHGlobal(IntPtr((void*)chars)); } //System::String转std::wstring void MarshalString ( String* s, wstring& os ) { const wchar_t* chars = (const wchar_t*)(Marshal::StringToHGlobalUni(s)).ToPointer(); os = chars; Marshal::FreeHGlobal(IntPtr((void*)chars)); } //System::String转std::string string str = ""; String* s = "abcdef"; const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer(); str = s; //std::wstring转System::String string str = "abcdef"; String* s; s = new String(str.c_str()); //System::String转char*; String* s = "abcdef"; char* ch= (char*)(void*)Marshal::StringToHGlobalAnsi(s); //System::String转char[]; char ch2[100]; strcpy(ch2,ch);