将String ^转换为wstring C ++ [英] Convert a String^ to wstring C++

查看:269
本文介绍了将String ^转换为wstring C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用C ++编写了一个小应用程序。 UI中有一个ListBox。而且我想将ListBox的选定项用于只能使用wstrings的算法。

I programmed a little Application in C++. There is a ListBox in the UI. And I want to use the selected Item of ListBox for an Algorithm where I can use only wstrings.

总而言之,我有两个问题:
-如何我将

All in all I have two questions: -how can I convert my

    String^ curItem = listBox2->SelectedItem->ToString();

进行wstring测试?

to a wstring test?

-在代码中^是什么意思?

-What means the ^ in the code?

非常感谢!

推荐答案

它应该像这样简单:

std::wstring result = msclr::interop::marshal_as<std::wstring>(curItem);

您还需要头文件来完成该工作:

You'll also need header files to make that work:

#include <msclr\marshal.h>
#include <msclr\marshal_cppstd.h>






这是什么 marshal_as 专业化看起来像是内部,出于好奇:


What this marshal_as specialization looks like inside, for the curious:

#include <vcclr.h>
pin_ptr<WCHAR> content = PtrToStringChars(curItem);
std::wstring result(content, curItem->Length);

之所以有效,是因为 System :: String 是内部存储为宽字符。如果您想使用 std :: string ,则必须执行Unicode转换,例如 WideCharToMultiByte 。方便地, marshal_as 会为您处理所有详细信息。

This works because System::String is stored as wide characters internally. If you wanted a std::string, you'd have to perform Unicode conversion with e.g. WideCharToMultiByte. Convenient that marshal_as handles all the details for you.

这篇关于将String ^转换为wstring C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆