我發現這非常好的一段代碼,將一個字符串轉換System:String^
如:C++/CLI字符串轉換
System::String^ rtn = gcnew String(move.c_str()); // 'move' here is the string
我傳遞RTN回C#程序。無論如何,在這個代碼存在的函數中,我傳入一個System::String^
。我也發現了一些代碼到System:String^
轉換爲字符串使用下面的代碼:
pin_ptr<const wchar_t> wch = PtrToStringChars(cmd); // 'cmd' here is the System:String
size_t convertedChars = 0;
size_t sizeInBytes = ((cmd->Length + 1) * 2);
errno_t err = 0;
char *ch = (char *)malloc(sizeInBytes);
err = wcstombs_s(&convertedChars,ch, sizeInBytes,wch, sizeInBytes);
現在我可以用「頻道」作爲一個字符串。
然而,這似乎比使用gcnew
轉換其他方式更多的工作。所以,最後我的問題是,有沒有什麼東西可以用gcnew的方式將System::String^
轉換爲字符串?
他們讓你做更多的工作,以故意做一些討厭這樣的。至少必須轉換爲utf8,UTF8Encoding :: GetBytes()。 – 2011-05-30 18:28:44