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

reading bytes of data from binary file

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 1.2k Views
  • 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.
  • K Offline
    K Offline
    kook
    wrote on 1 Oct 2021, 05:18 last edited by
    #1

    Greetings..
    I have two files one is having binary data and other file has structure of the messages

    I need to open the binary file and based on the data type of the message i need to read bytes of data from the mentioned offset and store that back into the message

    this is how the file which has structure of the message look like
    Screenshot from 2021-10-01 10-42-34.png
    (eg: read 4 bytes of data from 0th offset that is 0-3 bytes from binary file and store that value in msg_1)
    please somebody help me implementing this

    Thanks in advance

    J 1 Reply Last reply 1 Oct 2021, 05:24
    0
    • K kook
      1 Oct 2021, 05:18

      Greetings..
      I have two files one is having binary data and other file has structure of the messages

      I need to open the binary file and based on the data type of the message i need to read bytes of data from the mentioned offset and store that back into the message

      this is how the file which has structure of the message look like
      Screenshot from 2021-10-01 10-42-34.png
      (eg: read 4 bytes of data from 0th offset that is 0-3 bytes from binary file and store that value in msg_1)
      please somebody help me implementing this

      Thanks in advance

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 1 Oct 2021, 05:24 last edited by
      #2

      @kook said in reading bytes of data from binary file:

      please somebody help me implementing this

      Help with what exactly? What did you try so far? What is not clear?
      Reading arbitrary binary data from a file can be done with https://doc.qt.io/qt-5/qfile.html
      If you are the one who creates the binary files you should consider using https://doc.qt.io/qt-5/qdatastream.html

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      K 1 Reply Last reply 1 Oct 2021, 05:33
      1
      • J jsulm
        1 Oct 2021, 05:24

        @kook said in reading bytes of data from binary file:

        please somebody help me implementing this

        Help with what exactly? What did you try so far? What is not clear?
        Reading arbitrary binary data from a file can be done with https://doc.qt.io/qt-5/qfile.html
        If you are the one who creates the binary files you should consider using https://doc.qt.io/qt-5/qdatastream.html

        K Offline
        K Offline
        kook
        wrote on 1 Oct 2021, 05:33 last edited by
        #3

        @jsulm i forgot to mention what i have done so for
        i am not creating the binary file it is already there i just read it
        this is how i am reading it

        void MainWindow::on_actionOpen_triggered()
        {
        //opening file to read
        QString fileName = QFileDialog::getOpenFileName(this, "Open the file");
        QFile file(fileName);

        //validating
        if (!file.open(QIODevice::ReadOnly))
        {
            QMessageBox::warning(this, "Warning", file.errorString());
        }
        
        file.seek(16); // mentioning the offset
        QByteArray bytes = file.read(4); //how many bytes i want to read
        qDebug() << bytes.toHex();
        

        }

        what i am not understanding is how i will read that offset and data type from the message.txt ( i have to read these values and pass )
        file and store the read value back to the message

        J 1 Reply Last reply 1 Oct 2021, 05:39
        0
        • K kook
          1 Oct 2021, 05:33

          @jsulm i forgot to mention what i have done so for
          i am not creating the binary file it is already there i just read it
          this is how i am reading it

          void MainWindow::on_actionOpen_triggered()
          {
          //opening file to read
          QString fileName = QFileDialog::getOpenFileName(this, "Open the file");
          QFile file(fileName);

          //validating
          if (!file.open(QIODevice::ReadOnly))
          {
              QMessageBox::warning(this, "Warning", file.errorString());
          }
          
          file.seek(16); // mentioning the offset
          QByteArray bytes = file.read(4); //how many bytes i want to read
          qDebug() << bytes.toHex();
          

          }

          what i am not understanding is how i will read that offset and data type from the message.txt ( i have to read these values and pass )
          file and store the read value back to the message

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 1 Oct 2021, 05:39 last edited by
          #4

          @kook said in reading bytes of data from binary file:

          how i will read that offset and data type from the message.txt

          https://doc.qt.io/qt-5/qtextstream.html
          You will also need to parse the text file to get needed information.
          In your case it seems to be rather simple:

          • Read next line
          • Split the line at ':' (https://doc.qt.io/qt-5/qstring.html#split-5)
          • Check first column ([0]): if it is "datatype" then you know that the line contains information about data type, column [2] will contain its name
          • If first column does not contain "datatype" then it contains the type name of the message, message name is in second column ([1]), its offset in third ([2])

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          K 2 Replies Last reply 1 Oct 2021, 06:51
          2
          • J jsulm
            1 Oct 2021, 05:39

            @kook said in reading bytes of data from binary file:

            how i will read that offset and data type from the message.txt

            https://doc.qt.io/qt-5/qtextstream.html
            You will also need to parse the text file to get needed information.
            In your case it seems to be rather simple:

            • Read next line
            • Split the line at ':' (https://doc.qt.io/qt-5/qstring.html#split-5)
            • Check first column ([0]): if it is "datatype" then you know that the line contains information about data type, column [2] will contain its name
            • If first column does not contain "datatype" then it contains the type name of the message, message name is in second column ([1]), its offset in third ([2])
            K Offline
            K Offline
            kook
            wrote on 1 Oct 2021, 06:51 last edited by
            #5

            @jsulm thanks for the response i will implement this and come back here if i have any doubts :)

            1 Reply Last reply
            0
            • J jsulm
              1 Oct 2021, 05:39

              @kook said in reading bytes of data from binary file:

              how i will read that offset and data type from the message.txt

              https://doc.qt.io/qt-5/qtextstream.html
              You will also need to parse the text file to get needed information.
              In your case it seems to be rather simple:

              • Read next line
              • Split the line at ':' (https://doc.qt.io/qt-5/qstring.html#split-5)
              • Check first column ([0]): if it is "datatype" then you know that the line contains information about data type, column [2] will contain its name
              • If first column does not contain "datatype" then it contains the type name of the message, message name is in second column ([1]), its offset in third ([2])
              K Offline
              K Offline
              kook
              wrote on 1 Oct 2021, 08:43 last edited by
              #6

              @jsulm i have implemented as per your suggestion please check if i am doing it in a correct way and if not i am open for suggestions to improve

              void MainWindow::on_actionOpen_triggered()
              {
              //opening log file to read
              QString fileName = QFileDialog::getOpenFileName(this, "Open the file");
              QFile file(fileName);

              //validating
              if (!file.open(QIODevice::ReadOnly))
              {
                  QMessageBox::warning(this, "Warning", file.errorString());
              }
              
              //opening msgdata file to read
              QFile file1("/home/documents/msg.txt");
              
              if(!file1.open(QIODevice::ReadOnly))
              {
                 qDebug() << "file not opened";
              
              }
              
              QTextStream in(&file1);
              QString line;
              int offset = 0;
              int no_bytes = 0;
              while (in.readLineInto(&line))
              {
                  QStringList list = line.split(QLatin1Char(':'), QString::SkipEmptyParts);
                  if (list[0] == "uint32")
                  {
                      no_bytes = 4;
                  }
                  else if ( list[0] == "uint16" )
                  {
                      no_bytes = 2;
                  }
                  offset  = list[2].toInt();
                  file.seek(offset);
                  QByteArray bytes = file.read(no_bytes);
                  qDebug() << bytes.toHex();
              
                  list[1] = QString(bytes);
              
                   qDebug() << list[1];
              }
              

              }

              and this part

              if (list[0] == "uint32")
              {
                     no_bytes = 4;
              }
              

              i don't think this should be like this since i have to compare for many datatypes
              please suggest a proper way to achive this

              J 1 Reply Last reply 1 Oct 2021, 08:49
              0
              • K kook
                1 Oct 2021, 08:43

                @jsulm i have implemented as per your suggestion please check if i am doing it in a correct way and if not i am open for suggestions to improve

                void MainWindow::on_actionOpen_triggered()
                {
                //opening log file to read
                QString fileName = QFileDialog::getOpenFileName(this, "Open the file");
                QFile file(fileName);

                //validating
                if (!file.open(QIODevice::ReadOnly))
                {
                    QMessageBox::warning(this, "Warning", file.errorString());
                }
                
                //opening msgdata file to read
                QFile file1("/home/documents/msg.txt");
                
                if(!file1.open(QIODevice::ReadOnly))
                {
                   qDebug() << "file not opened";
                
                }
                
                QTextStream in(&file1);
                QString line;
                int offset = 0;
                int no_bytes = 0;
                while (in.readLineInto(&line))
                {
                    QStringList list = line.split(QLatin1Char(':'), QString::SkipEmptyParts);
                    if (list[0] == "uint32")
                    {
                        no_bytes = 4;
                    }
                    else if ( list[0] == "uint16" )
                    {
                        no_bytes = 2;
                    }
                    offset  = list[2].toInt();
                    file.seek(offset);
                    QByteArray bytes = file.read(no_bytes);
                    qDebug() << bytes.toHex();
                
                    list[1] = QString(bytes);
                
                     qDebug() << list[1];
                }
                

                }

                and this part

                if (list[0] == "uint32")
                {
                       no_bytes = 4;
                }
                

                i don't think this should be like this since i have to compare for many datatypes
                please suggest a proper way to achive this

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 1 Oct 2021, 08:49 last edited by jsulm 10 Jan 2021, 08:49
                #7

                @kook said in reading bytes of data from binary file:

                please suggest a proper way to achive this

                Create a QMap<QString, int> and use it to get the number of bytes for each datatype without the need to have many if..else:

                // In your MainWindow class add this in private section:
                QMap<QString, int> dataTypes;
                
                // In MainWindow constructor initialize dataTypes:
                dataTypes["uint32"] = 4;
                dataTypes["uint16"] = 2;
                ...
                
                // In void MainWindow::on_actionOpen_triggered() get the number of bytes like this:
                no_bytes = dataTypes[list[0]];
                

                But this only makes a difference if you have many data types!

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                K 1 Reply Last reply 1 Oct 2021, 08:53
                1
                • J jsulm
                  1 Oct 2021, 08:49

                  @kook said in reading bytes of data from binary file:

                  please suggest a proper way to achive this

                  Create a QMap<QString, int> and use it to get the number of bytes for each datatype without the need to have many if..else:

                  // In your MainWindow class add this in private section:
                  QMap<QString, int> dataTypes;
                  
                  // In MainWindow constructor initialize dataTypes:
                  dataTypes["uint32"] = 4;
                  dataTypes["uint16"] = 2;
                  ...
                  
                  // In void MainWindow::on_actionOpen_triggered() get the number of bytes like this:
                  no_bytes = dataTypes[list[0]];
                  

                  But this only makes a difference if you have many data types!

                  K Offline
                  K Offline
                  kook
                  wrote on 1 Oct 2021, 08:53 last edited by
                  #8

                  @jsulm yes i have many data types this seems helpfull thank you :)

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    reniwqwil5
                    Banned
                    wrote on 2 Oct 2021, 00:23 last edited by
                    #9
                    This post is deleted!
                    1 Reply Last reply
                    0

                    1/9

                    1 Oct 2021, 05:18

                    • Login

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