[英]Why C++ msgpack-c adds number 13 (0x0D) before number 10 (0x0A), but C# MessagePack-CSharp does not?
I was trying to use MessagePack to serialize integers from 0 to 127 in C++ and in C# on Windows, but the results are not the same. 我试图使用MessagePack在C ++和Windows中的C#中将0到127之间的整数序列化,但是结果不一样。 msgpack-c inserts 0x0D between 0x09 and 0x0A, but MessagePack-CSharp does not.
msgpack-c在0x09和0x0A之间插入0x0D,但MessagePack-CSharp不插入。 Why is that?
这是为什么?
OS: Windows 10 作业系统:Windows 10
IDE: Visual Studio 2019 IDE:Visual Studio 2019
C# C#
library: 图书馆:
https://github.com/neuecc/MessagePack-CSharp https://github.com/neuecc/MessagePack-CSharp
code: 码:
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);
}
}
}
result: 结果:
C++ C ++
library: 图书馆:
https://github.com/msgpack/msgpack-c https://github.com/msgpack/msgpack-c
code: 码:
#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();
}
result: 结果:
Since you open the file in c++ in text mode then every \\n
(ASCII 10) will have \\r
(ASCII 13) prepended if it doesn't exist on Windows. 由于您是在C ++中以文本模式打开文件的,因此,如果Windows中不存在
\\n
(ASCII 10),则每个\\n
(ASCII 10)都会带有\\r
(ASCII 13)。 You need to open the file in binary mode for this to not happen. 您需要以二进制模式打开文件,以免发生这种情况。
OutputFileStream.open("Cpp.msgpack", std::ofstream::binary);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.