samedi 18 mars 2017

Why does C# receive BigEndianUnicode Encoding from Visual Studio 2017 MFC C++ Project

I'm using named pipes to communicate between a C++ and C# application. I was surprised that the C# application encodes the umlaut ("ü") correctly when using Encoding.BigEndianUnicode.GetString but not with Encoding.Unicode.GetString.

The project uses the Unicode Character Set and MFC in a static library. Will the BigEndianUnicode encoding change when we remove MFC? Is there an easy way to switch this over so that it is correctly encoded with Encoding.Unicode.GetString?

I'm using the workaround for Microsoft's implementation of std::codecvt shown here: StackOverflow: Visual Studio C++ 2015 std::codecvt with char16_t or char32_t

u16string item = u"Testing ü";
std::wstring_convert<std::codecvt_utf16<int16_t>, int16_t> conv;
auto p = reinterpret_cast<const int16_t *>(item.data());
std::string utf16 = conv.to_bytes(p, p + item.size());
auto length = static_cast<int>(utf16.length());
boost::asio::write(stream, boost::asio::buffer(&length, sizeof length));        
boost::asio::write(stream, boost::asio::buffer(utf16.c_str(), utf16.size()));

On the C# end, this code snippet correctly encodes "Testing ü":

var length = ReadUInt32();
var data = ReadBytes((Int32)length);
string myString = Encoding.BigEndianUnicode.GetString(data);

Aucun commentaire:

Enregistrer un commentaire