简体   繁体   English

使用C#序列化二进制字符串,然后使用C ++反序列化

[英]Serialize a string in binary with C# and deserialize it with C++

I'm struggling to find an effective way to serialize a string that could contain both unicode and non-unicode characters into a binary array which I then serialize to a file that I have to deserialize using C++. 我正在努力寻找一种有效的方法来将可能包含Unicode和非Unicode字符的字符串序列化为二进制数组,然后将其序列化为必须使用C ++反序列化的文件。

I have already implemented a serializer/deserializer in C++ which I use to do most of my serialization which can handle both unicode and non-unicode characters (basically I convert non-unicode characters into their unicode equivalent and serialize everything as a unicode string, not the most effective way since every string now has 2 bytes per character but works). 我已经在C ++中实现了序列化器/反序列化器,我用它来完成大部分序列化工作,该序列化可以处理Unicode和非Unicode字符(基本上,我将非Unicode字符转换为等效的Unicode,并将所有内容序列化为Unicode字符串,而不是最有效的方法,因为每个字符串现在每个字符有2个字节,但可以工作)。

What I'm trying to achieve is to transform an arbitrary string into a 2 byte per character string that I can then deserialize from C++. 我要实现的目标是将任意字符串转换为每个字符串2个字节,然后可以从C ++反序列化。

What would be the most effective effective way to achieve what I'm looking for? 什么是实现我所寻找的最有效的有效方法?

Also, any suggestion regarding the way I'm serializing strings is well accepted of course. 另外,关于我对字符串进行序列化的方式的任何建议当然都是可以接受的。

Encoding.Unicode.GetBytes("my string") encodes the string as UTF-16, which has a size of 2 Bytes for each character. Encoding.Unicode.GetBytes("my string")将字符串编码为UTF-16,每个字符的大小为2个字节。 So if you are searching still an alternative consider the encoding. 因此,如果仍在搜索,请考虑编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM