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, 15:59 last edited by HB76
    #4

    Thank you for your response,

    indeed I noticed the pointer problem in my code, but even when I use the normal variable, the value returned is not correct :

    QDataStream s(&file);
    s.setByteOrder(QDataStream::LittleEndian);
    char buffer[4];
    
    s.skipRawData(80);
    s.readRawData(buffer,4);
    ulong length = ulong(buffer);
    qDebug() << length;
    

    Should return 70540 in this case but return 2686300.
    Same for :

    QDataStream s(&file);
    s.setByteOrder(QDataStream::LittleEndian);
    char *buffer = new char[4];
    
    s.skipRawData(80);
    s.readRawData(buffer,4);
    ulong length = ulong(*buffer);
    qDebug() << length;
    

    wich return 0...

    I would just like to understand what is incorrect in this code to know what I am doing wrong

    J 1 Reply Last reply 27 Mar 2020, 16:06
    0
    • H HB76
      27 Mar 2020, 15:59

      Thank you for your response,

      indeed I noticed the pointer problem in my code, but even when I use the normal variable, the value returned is not correct :

      QDataStream s(&file);
      s.setByteOrder(QDataStream::LittleEndian);
      char buffer[4];
      
      s.skipRawData(80);
      s.readRawData(buffer,4);
      ulong length = ulong(buffer);
      qDebug() << length;
      

      Should return 70540 in this case but return 2686300.
      Same for :

      QDataStream s(&file);
      s.setByteOrder(QDataStream::LittleEndian);
      char *buffer = new char[4];
      
      s.skipRawData(80);
      s.readRawData(buffer,4);
      ulong length = ulong(*buffer);
      qDebug() << length;
      

      wich return 0...

      I would just like to understand what is incorrect in this code to know what I am doing wrong

      J Offline
      J Offline
      JonB
      wrote on 27 Mar 2020, 16:06 last edited by JonB
      #5

      @HB76 said in Read binary data from file:
      ulong length = ulong(*buffer);

      only looks at the first char/byte in buffer, i.e. buffer[0], and that's probably a 0, so that's not much good!

      As for ulong length = ulong(buffer); not returning what you expect, the first thing I'd do is print it out in hex rather than decimal/look at the 4 bytes and see if they are the wrong way round/not what you expect in the 4 bytes. [I'm tired now, but doesn't ulong(buffer) just do the same thing as (ulong)buffer, and hence all you're printing is the address of the variable and nothing about its content?]

      Apart from that, and I haven't looked up the mechanics, but I don't see how QDataStream::setByteOrder() can cooperate with your choice of using QDataStream::readRawData(). If it reads raw bytes it's not going to be dealing with numbers and observing any byte ordering. Presumably you have to use QDataStream &QDataStream::operator>>(quint32 &i), https://doc.qt.io/qt-5/qdatastream.html#operator-gt-gt-5, if you want to read what you know to be numbers and have the byte order taken into account.

      1 Reply Last reply
      0
      • 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 Online
              C Online
              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 Online
                    C Online
                    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 Online
                        C Online
                        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

                          13/15

                          27 Mar 2020, 16:51

                          • Login

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