从std::string转换为string^ 我在C++中有一个函数,它在STD::string类型中有一个值,并希望将其转换为Str.^/P> void(String ^outValue) { std::string str("Hello World"); outValue = str; }

从std::string转换为string^ 我在C++中有一个函数,它在STD::string类型中有一个值,并希望将其转换为Str.^/P> void(String ^outValue) { std::string str("Hello World"); outValue = str; },string,c++-cli,managed-c++,String,C++ Cli,Managed C++,谷歌揭示(未经测试): //marshal\u as\u test.cpp //使用:/clr编译 #包括 #包括 #包括 使用名称空间系统; 使用名称空间msclr::interop; int main(){ std::string message=“要封送的测试字符串”; 字符串^result; 结果=封送(消息); 返回0; } 另请参见。谷歌搜索揭示(未经测试): //marshal\u as\u test.cpp //使用:/clr编译 #包括 #包括 #包括 使用名称空间系统; 使

谷歌揭示(未经测试):

//marshal\u as\u test.cpp
//使用:/clr编译
#包括
#包括
#包括
使用名称空间系统;
使用名称空间msclr::interop;
int main(){
std::string message=“要封送的测试字符串”;
字符串^result;
结果=封送(消息);
返回0;
}
另请参见。

谷歌搜索揭示(未经测试):

//marshal\u as\u test.cpp
//使用:/clr编译
#包括
#包括
#包括
使用名称空间系统;
使用名称空间msclr::interop;
int main(){
std::string message=“要封送的测试字符串”;
字符串^result;
结果=封送(消息);
返回0;
}
另请参见MSDN中的。

#include <string>
#include <iostream>
using namespace System;
using namespace std;

int main() {
   string str = "test";
   String^ newSystemString = gcnew String(str.c_str());
}
#包括
#包括
使用名称空间系统;
使用名称空间std;
int main(){
string str=“test”;
String^newSystemString=gcnewstring(str.c_str());
}

来自MSDN:

#include <string>
#include <iostream>
using namespace System;
using namespace std;

int main() {
   string str = "test";
   String^ newSystemString = gcnew String(str.c_str());
}
#包括
#包括
使用名称空间系统;
使用名称空间std;
int main(){
string str=“test”;
String^newSystemString=gcnewstring(str.c_str());
}

据我所知,至少封送处理方法(不确定新字符串)会导致std::字符串中的非ASCII UTF-8字符被破坏

根据我在上面的发现,我构建了这个解决方案,它至少在德语发音方面对我有效:

System::String^ StdStringToUTF16(std::string s)
{

 cli::array<System::Byte>^ a = gcnew cli::array<System::Byte>(s.length());
 int i = s.length();
 while (i-- > 0)
 {
    a[i] = s[i];
 }

 return System::Text::Encoding::UTF8->GetString(a);
}
System::String^StdStringToUTF16(std::String s)
{
cli::array ^a=gcnew cli::array(s.length());
int i=s.长度();
而(i-->0)
{
a[i]=s[i];
}
返回系统::文本::编码::UTF8->GetString(a);
}

据我所知,至少封送处理方法(不确定新字符串)会导致std::字符串中的非ASCII UTF-8字符被破坏

根据我在上面的发现,我构建了这个解决方案,它至少在德语发音方面对我有效:

System::String^ StdStringToUTF16(std::string s)
{

 cli::array<System::Byte>^ a = gcnew cli::array<System::Byte>(s.length());
 int i = s.length();
 while (i-- > 0)
 {
    a[i] = s[i];
 }

 return System::Text::Encoding::UTF8->GetString(a);
}
System::String^StdStringToUTF16(std::String s)
{
cli::array ^a=gcnew cli::array(s.length());
int i=s.长度();
而(i-->0)
{
a[i]=s[i];
}
返回系统::文本::编码::UTF8->GetString(a);
}

我想你需要另一个标签,因为字符串^ 在C++中没有什么意义。这是管理C++语法。你的函数甚至不正确,没有标识符。问题是这个函数被用来把一个C++的值转换成C。program@juanchopanza它也存在于WinRT中,可能应该调用.c_str();关于std::string@JoachimPileborg嗯,我肯定有一个标签!我认为你需要另一个标签,因为字符串Stry在C++中没有什么意义。这是管理C++语法。你的函数甚至不正确,没有标识符。问题是这个函数被用来把一个C++的值转换成C。program@juanchopanza它也存在于WinRT中,可能应该调用.c_str();关于std::string@JoachimPileborg嗯,我肯定有一个标签!