我如何将 const wchar_t* 转换为 System::String?

Fre*_*der 6 .net string c++-cli wchar-t

我需要将我的 SHA1 (wchar_t*) 转换为普通的 String^ 以便在某个函数中使用它。有任何想法吗?我试过谷歌,但所有的结果都与我的问题完全相反。:\

注意:我正在使用 C++.NET 框架和 Windows 窗体应用程序

Ðаn*_*Ðаn 7

使用构造函数;像这样:

const wchar_t* const pStr1 = ...;
System::String^ const str1 = gcnew System::String(pStr1);

const char* const pStr2 = ...;
System::String^ const str2 = gcnew System::String(pStr2);
Run Code Online (Sandbox Code Playgroud)

如果您使用标准 C++ 字符串类(std::wstringstd::string),则可以通过该c_str()方法获取指针。你的代码可能是

const std::wstring const std_str1 = ...;
System::String^ const str1 = gcnew System::String(std_str1.c_str());
Run Code Online (Sandbox Code Playgroud)

请参阅System.String此处的广泛讨论。