Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Convert a QByteArray in Hex to a Struct
Qt 6.11 is out! See what's new in the release blog

Convert a QByteArray in Hex to a Struct

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 5 Posters 2.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • DSpiderD DSpider

    Hello,

    I am reading an hex chain from a file with QDataStream. I have to put this hex chain into
    a struct, I am trying to do it like this:

    QDataStream in(fileData);
    
    QByteArray header(sizeof(struct header), Qt::Uninitialized);
    struct header *hdd;
    
    in.readRawData(header.data(), sizeof(struct header));
    
    hdd = reinterpret_cast<struct header *>(header.toHex().data());]
    

    This is not working unfortunately, is there a way to do so ? otherwise I should decode members of the struct one by one and assign them individually.

    Thank you in advance

    EDIT

    Some precisions:

    The file is a binary.
    Here is the structure:

    struct header{
        int ver_global;
        int ver_qdatastream;
        int sampling_freq;
        quint8 tag;
        int nb_analog_input;
        int bloc_size;
    
        int ver_param;
        int year;
        int month;
        int day;
        int hour;
        int minutes;
        int seconds;
        int milliseconds;
        QString patient_fst_name;
        QString patient_lst_name;
        int year_birth;
        int month_birth;
        int day_birth;
    
        int nb_analog_inputs;
        int ver_calib;
        int ver_qdatastream2;
    
        // Val1
        QString input1_name;
        QString input1_unit;
        short input1_precision;
        int input1_min;
        int input1_max;
        double input1_nb_unit_at_0;
        double input1_nb_unit_by_volt;
        double input1_val;
        bool input1_validity;
        bool input1_status;
        QString input1_name_abr;
    
        // Val2
        QString input2_name;
        QString input2_unit;
        short input2_precision;
        int input2_min;
        int input2_max;
        double input2_nb_unit_at_0;
        double input2_nb_unit_by_volt;
        double input2_val;
        bool input2_validity;
        bool input2_status;
        QString input2_name_abr;
    
        // Val3
        QString input3_name;
        QString input3_unit;
        short input3_precision;
        int input3_min;
        int input3_max;
        double input3_nb_unit_at_0;
        double input3_nb_unit_by_volt;
        double input3_val;
        bool input3_validity;
        bool input3_status;
        QString input3_name_abr;
    
        // Val4
        QString input4_name;
        QString input4_unit;
        short input4_precision;
        int input4_min;
        int input4_max;
        double input4_nb_unit_at_0;
        double input4_nb_unit_by_volt;
        double input4_val;
        bool input4_validity;
        bool input4_status;
        QString input4_name_abr;
    
    
    };
    
    aha_1980A Offline
    aha_1980A Offline
    aha_1980
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @DSpider thats most likely possible, but to tell more we'd need to see the struct definition.

    Also, is the file a binary or an ASCII file with hex chars?

    Qt has to stay free or it will die.

    DSpiderD 1 Reply Last reply
    1
    • aha_1980A aha_1980

      @DSpider thats most likely possible, but to tell more we'd need to see the struct definition.

      Also, is the file a binary or an ASCII file with hex chars?

      DSpiderD Offline
      DSpiderD Offline
      DSpider
      wrote on last edited by
      #3

      @aha_1980 Thank you. I just edited the question.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #4

        Hi,

        How are you writing the structure to file in the first place ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        DSpiderD 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          How are you writing the structure to file in the first place ?

          DSpiderD Offline
          DSpiderD Offline
          DSpider
          wrote on last edited by
          #5

          @SGaist I actually don't know. I am supposed to read only, another program (I don't have access to) does the writting.

          mrjjM 1 Reply Last reply
          0
          • DSpiderD DSpider

            @SGaist I actually don't know. I am supposed to read only, another program (I don't have access to) does the writting.

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #6

            @DSpider
            So its very unlikely that other program used QString ?
            or its an Qt app too ?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #7

              Would it be possible for you to request to know how it's done ?

              If the writing is done using QDataStream it will simplify things.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              DSpiderD 1 Reply Last reply
              2
              • DSpiderD Offline
                DSpiderD Offline
                DSpider
                wrote on last edited by
                #8

                @mrjj well its a Qt app too, but I don't have more information. I have the structure of the file, I am able to decode it using QDataStream if I do it element by element. But I was wondering if there is a more simpler way to it ?

                1 Reply Last reply
                0
                • SGaistS SGaist

                  Would it be possible for you to request to know how it's done ?

                  If the writing is done using QDataStream it will simplify things.

                  DSpiderD Offline
                  DSpiderD Offline
                  DSpider
                  wrote on last edited by
                  #9

                  @SGaist I think it is done with QDataStream, because in the struct there is even the version of it. Supposing it is the case, how can I do then ?

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10
                    QDataStream in(fileData);
                    
                    struct header;
                    in >> header;
                    

                    You should check whether the QDataStream used has its version set. If not, then you should suggest to add that so that you can ensure that if a future version of Qt changes some aspect of the QDataStream protocol you can still read the old files you have generated.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    DSpiderD 1 Reply Last reply
                    3
                    • SGaistS SGaist
                      QDataStream in(fileData);
                      
                      struct header;
                      in >> header;
                      

                      You should check whether the QDataStream used has its version set. If not, then you should suggest to add that so that you can ensure that if a future version of Qt changes some aspect of the QDataStream protocol you can still read the old files you have generated.

                      DSpiderD Offline
                      DSpiderD Offline
                      DSpider
                      wrote on last edited by
                      #11

                      @SGaist Thank you, I 'll try that right now. However, the header is actually on the first 1024 bytes of the file. Is there a way to read only this part ?

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        if the writing was done like this:

                        QDataStream out(file);
                        out << myStruct:
                        

                        then there's nothing special to do.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        DSpiderD 1 Reply Last reply
                        2
                        • SGaistS SGaist

                          if the writing was done like this:

                          QDataStream out(file);
                          out << myStruct:
                          

                          then there's nothing special to do.

                          DSpiderD Offline
                          DSpiderD Offline
                          DSpider
                          wrote on last edited by
                          #13

                          @SGaist I tried to do it this way but it says "invalid operand to binary expression ('QDataStream' and 'struct header').

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #14

                            I knew I was forgetting something, you have to implement the QDataStream operator for the structure.

                            Which is something that has likely be done for the other application. The operators implementation boils down to sending each field to the output stream and read each field from the input stream.

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            DSpiderD 1 Reply Last reply
                            2
                            • fcarneyF Offline
                              fcarneyF Offline
                              fcarney
                              wrote on last edited by fcarney
                              #15

                              @SGaist said in Convert a QByteArray in Hex to a Struct:

                              The operators implementation boils down to sending each field to the output stream and read each field from the input stream.

                              I was wondering about that. I read a bunch of articles last week about transferring binary data. Two of the major concerns are binary alignment/packing and endianess. Does the QDataStream address both of those issues? I am more concerned with the alignment/packing as all our systems are little endian. The alignment/packing can change if compiler settings are changed.

                              Edit:
                              Sorry, I should have just looked it up:
                              http://doc.qt.io/qt-5/qdatastream.html

                              C++ is a perfectly valid school of magic.

                              1 Reply Last reply
                              0
                              • SGaistS SGaist

                                I knew I was forgetting something, you have to implement the QDataStream operator for the structure.

                                Which is something that has likely be done for the other application. The operators implementation boils down to sending each field to the output stream and read each field from the input stream.

                                DSpiderD Offline
                                DSpiderD Offline
                                DSpider
                                wrote on last edited by
                                #16

                                @SGaist Thank you. So it should look something like this ( which I am writting now):

                                friend QDataStream &operator>> (QDataStream &in, header &head)
                                    {
                                        in >> head.field1 >> head.field2.....;
                                        return in;
                                    }
                                
                                1 Reply Last reply
                                1
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #17

                                  Yes it is, but you have to ensure the the writing was done in the same order.

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  DSpiderD 1 Reply Last reply
                                  0
                                  • SGaistS SGaist

                                    Yes it is, but you have to ensure the the writing was done in the same order.

                                    DSpiderD Offline
                                    DSpiderD Offline
                                    DSpider
                                    wrote on last edited by
                                    #18

                                    @SGaist Thanksssssssss to the infinity. That saved me tonight ! It's working.
                                    I'm a noob on Qt, and do not have any clue on how it works, even C++ I'm not very familiar too.
                                    Thank you all for your help.

                                    1 Reply Last reply
                                    0
                                    • SGaistS Offline
                                      SGaistS Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #19

                                      You're welcome !

                                      Qt is C++ so by learning the second you'll be able to master the first :)

                                      Interested in AI ? www.idiap.ch
                                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      1 Reply Last reply
                                      3

                                      • Login

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Users
                                      • Groups
                                      • Search
                                      • Get Qt Extensions
                                      • Unsolved