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. Read binary data from file
Forum Updated to NodeBB v4.3 + New Features

Read binary data from file

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 6 Posters 2.9k 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.
  • H Offline
    H Offline
    HB76
    wrote on 27 Mar 2020, 16:27 last edited by
    #6
    QDataStream s(&file);
    s.setByteOrder(QDataStream::LittleEndian);
    
    s.skipRawData(80);
    qint32 value;
    s >> value;
    qDebug() << value;
    

    give me the same result, 0...

    P 1 Reply Last reply 27 Mar 2020, 16:37
    0
    • H HB76
      27 Mar 2020, 16:27
      QDataStream s(&file);
      s.setByteOrder(QDataStream::LittleEndian);
      
      s.skipRawData(80);
      qint32 value;
      s >> value;
      qDebug() << value;
      

      give me the same result, 0...

      P Offline
      P Offline
      Pablo J. Rogina
      wrote on 27 Mar 2020, 16:37 last edited by
      #7

      @HB76 just in case, could you please post:

      1. the values of the bytes at positions 80, 81, 82, 83 for the file you're working with?
      2. the size of the data (or the whole file) for such file?

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • H Offline
        H Offline
        HB76
        wrote on 27 Mar 2020, 16:39 last edited by
        #8

        I've found a way to do it :

        QByteArray data = file.read(4);
        qint32 facet_count;
        memcpy(&facet_count, data.constData(), 4);
        qDebug() << facet_count;
        

        Just for knowledge, why this simple code doesn'y work ?

        file.seek(80);
        ulong length =  file.read(4).toULong();
        qDebug() << length;
        

        it would be much more simplier..

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 27 Mar 2020, 16:43 last edited by
          #9

          @HB76 said in Read binary data from file:

          ulong length = file.read(4).toULong();

          Because you did not read the documentation: https://doc.qt.io/qt-5/qbytearray.html#toLong

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          H 1 Reply Last reply 27 Mar 2020, 16:46
          2
          • H Offline
            H Offline
            HB76
            wrote on 27 Mar 2020, 16:43 last edited by
            #10

            @Pablo-J-Rogina the values are :

            80 : "\x8C"
            81 : "\x13"
            82 : "\x01"
            83 : "\x00"

            the file length is 3527084 using

            int length = int(file.size());
            
            1 Reply Last reply
            0
            • C Christian Ehrlicher
              27 Mar 2020, 16:43

              @HB76 said in Read binary data from file:

              ulong length = file.read(4).toULong();

              Because you did not read the documentation: https://doc.qt.io/qt-5/qbytearray.html#toLong

              H Offline
              H Offline
              HB76
              wrote on 27 Mar 2020, 16:46 last edited by
              #11

              @Christian-Ehrlicher I was just having a look at it but I supposed I didn't understand the whole explaination the first time as I'm not native english ^^'

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 27 Mar 2020, 16:47 last edited by Christian Ehrlicher
                #12

                The example there should be obvious:

                QByteArray str("FF");
                bool ok;
                int hex = str.toInt(&ok, 16);     // hex == 255, ok == true
                int dec = str.toInt(&ok, 10);     // dec == 0, ok == false
                

                As you can see it converts a string value into an integer

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  HB76
                  wrote on 27 Mar 2020, 16:51 last edited by
                  #13

                  ok I got it now, but why every time the conversion failed with this method whereas the conversion with memcpy is working ?

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 27 Mar 2020, 16:57 last edited by
                    #14

                    @HB76 said in Read binary data from file:

                    ut why every time the conversion failed with this method whereas the conversion with memcpy is working ?

                    Again: toUint() interprets your string as ascii text and tries to convert it to an integer, whereas memcpy simply copies the plain data - C basics on how a value is interpreted.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    2
                    • H Offline
                      H Offline
                      HB76
                      wrote on 27 Mar 2020, 16:59 last edited by
                      #15

                      Ok thank you very much !
                      I don't know if it is the most efficient way to read data but it is working !

                      1 Reply Last reply
                      0

                      15/15

                      27 Mar 2020, 16:59

                      • Login

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