Search code examples
c#c++msgpackbinary-serialization

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. msgpack-c inserts 0x0D between 0x09 and 0x0A, but MessagePack-CSharp does not. Why is that?

OS: Windows 10

IDE: Visual Studio 2019

C#

library:

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:

CSharp

C++

library:

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:

Cpp


Solution

  • 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. You need to open the file in binary mode for this to not happen.

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