Search code examples
c#c++qtbinaryreader

How to use BinaryReader class to read a custom file (C++) using C#?


Okay, so, for a personal project I've been given the task to code a tool to make use of a custom file that was serialized from C++ using C#.

I thought of using BinaryReader class and effectively that was the right path but as I suspected I couldn't get any useful info without the structs used to serialize the file, so I asked for them. The problem is that the info I was sent is 2 struct names containing all qint32 fields (yes they use Qt Library).

To be honest I'm completely lost with this, also I'd like to know if qint32 is Qt's int32 representation or if it's effectively different from it.

Regards.


Solution

  • According to this question and answer a qint32 is 32 bits in length.

    That means you can use BinaryReader.ReadInt32() to read the numbers assuming they are little endian.

    The definition for qint32 from qglobal.h is as follows:

    typedef int qint32;                /* 32 bit signed */
    

    The C++ standard does not specify the endianness of numbers, instead the architecture the program is compiled on does. x86 and x64 processors are little endian. Note that it is possible to change the endianness of a number (e.g. by using the htonl function). To get a definitive answer, you should ask whoever generated the file.