使用marshal_as和编码的字符串编组 [英] String marshalling with marshal_as and encodings

查看:665
本文介绍了使用marshal_as和编码的字符串编组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在string ^和std :: string之间转换本质上很容易使用marshal_as。然而,我没有找到一个描述如何处理这样的字符串中的编码。 String ^使用UTF-16,但是关于std :: string?在其中的文本可以以各种方式解释,如果编组将转换为本地的应用程序的编码,将是非常有用的。

Converting between String^ and std::string is essentially easy using marshal_as. However, I have nowhere found a description of how encodings in such a string are handled. String^ uses UTF-16 but what about std::string? Text in it can be interpreted in various ways and it would be very usefull if the marshalling would convert in an encoding that is native to your application.

在我的case all std :: string实例包含UTF-8编码的文本。那么我如何告诉marshal_as给我一个UTF-8编码的变体的原始String ^(反之亦然)?

In my case all std::string instances contain UTF-8 encoded text. So how would I tell marshal_as to give me an UTF-8 encoded variant of the original String^ (and vice versa)?

推荐答案

我同意文档缺乏。如果没有适当的文档,我们会巧合编程 marshal_as 可能非常有用,但是当我有一个问题,在文档中没有回答,我只是跳过它,并在多个步骤。有人可能有一个关于 marshal_as 在每种情况下如何工作的准确答案,但除非将其添加到您的代码作为注释,下一个程序员不会想到的问题或者在检查文档后也能理解它。

I agree that the documentation is lacking. Without proper documentation we are programming by coincidence. marshal_as can be very useful but when I have a question that isn't answered in the documentation, I just skip it and do it in multiple steps. Someone may have an accurate answer about how marshal_as works in each case but unless you add it to your code as a comment, the next programmer isn't going to think of the issue or understand it, even after checking the documentation.

BCL非常有能力转换字符。我建议使用 Encoding 成员到 GetBytes ,然后将它们复制到C或C ++字符串数据结构/类。尽管需要更多的步骤,然后清楚你正在使用哪些字符集和编码,如何处理不匹配,如何传输字符串所有权以及如何销毁它。 (当在UTF-16和UTF-8之间转换时,不匹配当然不适用。)

The BCL is very capable of converting characters. I suggest using an Encoding member to GetBytes and then copy them to a C or C++ string data structure/class. Despite requiring more steps, it is then clear which character sets and encodings you are using, how mismatches are handled, how the string ownership can be transfered and how it should be destroyed. (Mismatches are, of course, not applicable when converting between UTF-16 and UTF-8.)

这篇关于使用marshal_as和编码的字符串编组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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