如何在C++ .NET中将System::String转换为std::string?
如何在C++ .NET中将System::String转换为std::string?
如果您使用的是最新版本的 .net,那么语法更加简洁。
#include "stdafx.h"
#include <string>
#include <msclr\marshal_cppstd.h>
using namespace System;
int main(array<System::String ^> ^args)
{
System::String^ managedString = "test";
msclr::interop::marshal_context context;
std::string standardString = context.marshal_as<std::string>(managedString);
return 0;
}
这也可以在异常处理方面提供更好的清理。
还有一个MSDN文章提供其他各种类型的转换。
msclr::interop::marshal_context
中使用了Win32 API WideCharToMultiByte
,它最终转换为Windows-1252编码,至少根据我的经验。可能有方法可以避开这个问题,但我认为如果你想要UTF-8,那么你需要自己处理。例如,英镑符号'£'被转换为单字节0xA3,而拉丁字母Q 'ʠ' 被转换为单个“?”符号。无论C++/CLI项目的字符集设置是MBCS还是Unicode,它都会以这种方式运行。 - Mike E而对于C++/CLI的较新版本中关于更简单的方法,你可以不使用marshal_context来完成。我知道这在Visual Studio 2010中是可行的,但之前的情况就不太确定了。
#include "stdafx.h"
#include <string>
#include <msclr\marshal_cppstd.h>
using namespace msclr::interop;
int main(array<System::String ^> ^args)
{
System::String^ managedString = "test";
std::string standardString = marshal_as<std::string>(managedString);
return 0;
}
std::string
的情况下,这是否需要呢? - Kristopher Johnsonstd :: wstring
始终表示 UTF16 。std :: string
则始终表示 UTF8 。#include "stdafx.h"
#include <string>
#include <msclr\marshal_cppstd.h>
using namespace System;
int main(array<System::String ^> ^args)
{
System::String^ managedString = "test";
msclr::interop::marshal_context context;
//Actual format is UTF16, so represent as wstring
std::wstring utf16NativeString = context.marshal_as<std::wstring>(managedString);
//C++11 format converter
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> convert;
//convert to UTF8 and std::string
std::string utf8NativeString = convert.to_bytes(utf16NativeString);
return 0;
}
或者使用更紧凑的语法:
int main(array<System::String ^> ^args)
{
System::String^ managedString = "test";
msclr::interop::marshal_context context;
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> convert;
std::string utf8NativeString = convert.to_bytes(context.marshal_as<std::wstring>(managedString));
return 0;
}
stdString = toss(systemString);
static std::string toss( System::String ^ s )
{
// convert .NET System::String to std::string
const char* cstr = (const char*) (Marshal::StringToHGlobalAnsi(s)).ToPointer();
std::string sstr = cstr;
Marshal::FreeHGlobal(System::IntPtr((void*)cstr));
return sstr;
}
我之前遇到了太多模糊的错误(是的,我是一个C++新手),以上回答对我来说都不适用。
这个方法对我在C#和C++ CLI之间传递字符串很有用。
C#
bool result;
result = mps.Import(mpsToolName);
C++ CLI
功能:
bool ManagedMPS::Import(System::String^ mpsToolNameTest)
std::string mpsToolName;
mpsToolName = toStandardString(mpsToolNameTest);
将String^转换为std::string的函数
static std::string toStandardString(System::String^ string)
{
using System::Runtime::InteropServices::Marshal;
System::IntPtr pointer = Marshal::StringToHGlobalAnsi(string);
char* charPointer = reinterpret_cast<char*>(pointer.ToPointer());
std::string returnString(charPointer, string->Length);
Marshal::FreeHGlobal(pointer);
return returnString;
}
std::string Utils::ToUnmanagedString(String^ stringIncoming)
{
std::string unmanagedString = marshal_as<std::string>(stringIncoming);
return unmanagedString;
}
创建一个可供使用的 Windows 运行时组件:
String^ systemString = "Hello";
std::wstring ws1(systemString ->Data());
std::string standardString(ws1.begin(), ws1.end());
Data()
方法。 - demberto
std::string
必须是 ASCII 编码(提示:它应该是 UTF-8 编码!) - Lightness Races in Orbit