Sort string using marshal_as and encodings

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

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

+3
winforms c ++ - cli
Sep 19 '13 at 12:24
source share
1 answer

I agree that there is no documentation. Without proper documentation, we are programming by coincidence . marshal_as can be very useful, but when I get a question that wasn’t answered in the documentation, I just skip it and do it in a few steps. Someone may have an exact answer about how marshal_as works in each case, but if you do not add it to your code as a comment, the next programmer is not going to think about this problem or understand it even after checking the documentation.

BCL is very capable of character conversion. I suggest using the Encoding member for GetBytes and then copying them into the string structure / string class of C or C ++. Despite the fact that more steps are required, then it is clear which character sets and encodings you use, how the inconsistencies are handled, how ownership of the string is transferred, and how it should be destroyed. (Mismatches, of course, do not apply when converting between UTF-16 and UTF-8.)

+3
Sep 20 '13 at 3:12
source share



All Articles