1. std::string 转成System.String
[csharp]
view plaincopy#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::stringvoid MarshalString ( String* s, string& os ) { const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer(); os = chars; Marshal::FreeHGlobal(IntPtr((void*)chars));}//System::String转std::wstringvoid 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::stringstring str = "";String* s = "abcdef";const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();str = s;//std::wstring转System::Stringstring 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);
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。