我发现这非常好的一段代码,将一个字符串转换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