C # uses the UTF16 format for its lines.
Thus, in addition to type conversion, you should also be aware of the actual string format.
When compiling for a multibyte character set, Visual Studio and the Win API assume UTF8 (actually Windows Windows-28591 encoding ). When compiling Unicode for a character set, Visual Studio and the Win API assume UTF16.
So, you should convert the string from UTF16 to UTF8 format, and not just convert to std :: string.
This will become necessary when working with multi-character formats, such as some non-Latin languages.
The idea is to decide that std::wstring always represents UTF16 .
And std::string always represents UTF8 .
This is not done by the compiler, it is rather a good policy.
#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; }
Or use a more compact syntax:
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; }
Yochai Timmer Dec 25 '15 at 9:42 2015-12-25 09:42
source share