C++/CLI 从 System::String^ 转换为 std::string
有人可以发布一个简单的代码来转换
System::String^
To,
C++ std::string
即,我只想分配
String^ originalString;
To 的值,
std::string newString;
Can someone please post a simple code that would convert,
System::String^
To,
C++ std::string
I.e., I just want to assign the value of,
String^ originalString;
To,
std::string newString;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
以下是我多年前为 C++/cli 项目编写的一些转换例程,它们应该仍然有效。
Here are some conversion routines I wrote many years ago for a c++/cli project, they should still work.
我发现从 String^ 获取 std::string 的简单方法是使用 sprintf()。
无需调用 Marshal 函数!
更新 感谢 Eric,我修改了示例代码来检查输入字符串的大小,以防止缓冲区溢出。
I found an easy way to get a std::string from a String^ is to use sprintf().
No need to call the Marshal functions!
UPDATE Thanks to Eric, I've modified the sample code to check for the size of the input string to prevent buffer overflow.
查看 System::Runtime::InteropServices::Marshal::StringToCoTaskMemUni() 及其朋友。
抱歉,现在无法发布代码; 我这台机器上没有 VS,无法在发布之前检查它的编译情况。
Check out
System::Runtime::InteropServices::Marshal::StringToCoTaskMemUni()
and its friends.Sorry can't post code now; I don't have VS on this machine to check it compiles before posting.
这对我有用:
This worked for me:
不要自己动手,使用这些方便(且可扩展)的包装器由微软提供。
例如:
Don't roll your own, use these handy (and extensible) wrappers provided by Microsoft.
For example:
您可以轻松地按如下方式执行此操作
You can easily do this as follows
C# 使用 UTF16 格式作为字符串。
因此,除了转换类型之外,您还应该注意字符串的实际格式。
编译多字节字符集时,Visual Studio 和 Win API 假定为 UTF8(实际上 Windows 编码为 Windows-28591 )。
针对Unicode 字符集进行编译时,Visual studio 和 Win API 假定为 UTF16。
因此,您还必须将字符串从 UTF16 格式转换为 UTF8 格式,而不仅仅是转换为 std::string。
当使用多字符格式(例如某些非拉丁语言)时,这将变得必要。
这个想法是决定
std::wstring
始终 代表 UTF16。std::string
always 代表UTF8。这不是由编译器强制执行的,它更像是一个好的策略。
或者使用更紧凑的语法:
C# uses the UTF16 format for its strings.
So, besides just converting the types, you should also be conscious about the string's actual format.
When compiling for Multi-byte Character set Visual Studio and the Win API assumes UTF8 (Actually windows encoding which is Windows-28591 ).
When compiling for Unicode Character set Visual studio and the Win API assume UTF16.
So, you must convert the string from UTF16 to UTF8 format as well, and not just convert to std::string.
This will become necessary when working with multi-character formats like some non-latin languages.
The idea is to decide that
std::wstring
always represents UTF16.And
std::string
always represents UTF8.This isn't enforced by the compiler, it's more of a good policy to have.
Or have it in a more compact syntax:
我喜欢远离编组员。
对我来说看起来更干净、更快。 无需担心创建和删除上下文。
I like to stay away from the marshaller.
Seems much cleaner and faster to me. No need to worry about creating and deleting a context.
// 我使用VS2012编写了以下代码--convert_system_string to Standard_Sting
// I used VS2012 to write below code-- convert_system_string to Standard_Sting
我花了几个小时尝试将 Windows 表单列表框 ToString 值转换为标准字符串,以便我可以将它与 fstream 一起使用以输出到 txt 文件。 我的 Visual Studio 没有附带 marshal 头文件,我发现有几个答案说可以使用这些头文件。 经过这么多次尝试和错误,我终于找到了仅使用 System::Runtime::InteropServices: 问题的解决方案:
下面是包含示例的 MSDN 页面:
http://msdn.microsoft.com/en-us/library/1b4az623 (v=vs.80).aspx
我知道这是一个非常简单的解决方案,但这花了我几个小时的时间进行故障排除并访问多个论坛,最终找到了有效的解决方案。
I spent hours trying to convert a windows form listbox ToString value to a standard string so that I could use it with fstream to output to a txt file. My Visual Studio didn't come with marshal header files which several answers I found said to use. After so much trial and error I finally found a solution to the problem that just uses System::Runtime::InteropServices:
And here is the MSDN page with the example:
http://msdn.microsoft.com/en-us/library/1b4az623(v=vs.80).aspx
I know it's a pretty simple solution but this took me HOURS of troubleshooting and visiting several forums to finally find something that worked.
对我来说,其中一些消息出现错误。 我有一个 std::string 。 要将其转换为 String^,我必须执行以下操作
String^ sysString = gcnew String(stdStr.c_str());
其中 sysString 是 System::String^ ,stdStr 是 std::细绳。 希望这对某人有帮助您可能必须
#include
才能正常工作For me, I was getting an error with some of these messages. I have an std::string. To convert it to String^, I had to do the following
String^ sysString = gcnew String(stdStr.c_str());
where sysString is a System::String^ and stdStr is an std::string. Hope this helps someoneYou may have to
#include <string>
for this to work