0

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

8
  • 1
    That is a carriage return. Commented May 13, 2019 at 15:22
  • But if I have a list of 128 integers serialized in a binary file, the result should be the same, should it not? Commented May 13, 2019 at 15:24
  • 1
    Oh. Well my guess is that the two pieces of software aren't guaranteed to be compatible with each other. Commented May 13, 2019 at 15:25
  • 3
    If you are serialising into a binary file, then you need to open the file in binary mode. Commented May 13, 2019 at 15:26
  • 1
    @RobertHarvey the perennial Unix vs MS-Windows line-ending issue. Commented May 13, 2019 at 15:28

1 Answer 1

5

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.