gpt4 book ai didi

c# - 为什么 C++ msgpack-c 在数字 10 (0x0A) 之前添加数字 13 (0x0D),而 C# MessagePack-CSharp 没有?

转载 作者:太空宇宙 更新时间:2023-11-04 15:56:39 26 4
gpt4 key购买 nike

我尝试使用 MessagePack 在 C++ 和 Windows 上的 C# 中序列化 0 到 127 之间的整数,但结果并不相同。 msgpack-c 在 0x09 和 0x0A 之间插入 0x0D,但 MessagePack-CSharp 没有。这是为什么?

操作系统:Windows 10

集成环境: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);
}
}
}

结果:

CSharp

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();
}

结果:

Cpp

最佳答案

由于您在文本模式下用 C++ 打开文件,因此如果每个 \n (ASCII 10) 不存在,都会在其前面加上 \r (ASCII 13)在 Windows 上。您需要以二进制模式打开文件,以免发生这种情况。

OutputFileStream.open("Cpp.msgpack", std::ofstream::binary);

关于c# - 为什么 C++ msgpack-c 在数字 10 (0x0A) 之前添加数字 13 (0x0D),而 C# MessagePack-CSharp 没有?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56115371/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com