我试图使用MessagePack在C ++和Windows的C#中将0到127的整数序列化,但是结果不一样。 msgpack-c在0x09和0x0A之间插入0x0D,但MessagePack-CSharp不插入。为什么会这样?
操作系统:Windows 10
IDE:Visual Studio 2019
C#
图书馆:
https://github.com/neuecc/MessagePack-CSharp
代码:
using System.Collections.Generic;
using System.IO;
class Program
{
static void Main(string[] args)
{
using (FileStream fileStream = new FileStream("CSharp.msgpack", FileMode.Create, FileAccess.Write))
{
List<int> list = new List<int>();
for (int i = 0; i < 128; ++i)
{
list.Add(i);
}
MessagePackSerializer.Serialize(fileStream, list);
}
}
}
结果:
C ++
图书馆:
https://github.com/msgpack/msgpack-c
代码:
#include <msgpack.hpp>
#include <vector>
#include <iostream>
#include <fstream>
int main(void)
{
std::ofstream OutputFileStream;
std::vector<int> list;
for (int i = 0; i < 128; ++i)
{
list.push_back(i);
}
OutputFileStream.open("Cpp.msgpack");
msgpack::pack(OutputFileStream, list);
OutputFileStream.close();
}
结果:
答案 0 :(得分:5)
由于您以文本模式在c ++中打开文件,因此如果Windows中不存在,每个\n
(ASCII 10)都将以\r
(ASCII 13)作为前缀。您需要以二进制模式打开文件,以免发生这种情况。
OutputFileStream.open("Cpp.msgpack", std::ofstream::binary);