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 Raw Data from File

Reading Raw Data from File

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 5 Posters 4.3k Views 1 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.
  • jipe3001J Offline
    jipe3001J Offline
    jipe3001
    wrote on last edited by VRonin
    #1

    Hi guys, I am trying to read a raw data file as follows:

     QFile fileName("C:/0data/QT Projects new/falcon/18071209.422");
    fileName.open(QIODevice::ReadOnly);
     QDataStream in(&fileName);
    
    QByteArray line_data(7,0);
    
    while (!in.atEnd()) {
    
     in.readRawData(line_data.data(),7);
    
               v_lap     = line_data.mid(0,1);
               v_data1   = line_data.mid(1,1);
               v_data2   = line_data.mid(2,1);
               v_data3   = line_data.mid(3,1);
               v_data4   = line_data.mid(4,1);
               v_datacr  = line_data.mid(5,1);
               v_datalf  = line_data.mid(6,1);
    
               vlap_max = v_lap.toInt();
    }
    

    And here are the results:

    Variables locales		
    	line_data	"\001\030\030\000\030\r\n"	QByteArray
    		v_data1	"\030"	QVariant (QByteArray)
    		v_data2	"\030"	QVariant (QByteArray)
    		v_data3	"\000"	QVariant (QByteArray)
    		v_data4	"\030"	QVariant (QByteArray)
    		v_datacr	"\r"	QString
    		v_datalf	"\n"	QString
    		v_lap	"\001"	QVariant (QByteArray)
    		vlap_max	0	qint16
    

    My concern is about the values who seems to be always seen as char despite a raw_data read and
    when trying to convert v_lap to int the result is always 0 !!

    Thks for your usual coop

    JP

    jsulmJ J.HilkJ 2 Replies Last reply
    0
    • jipe3001J Offline
      jipe3001J Offline
      jipe3001
      wrote on last edited by
      #9

      I finally succeed to read my file in Raw mode with QVariant as follows:

      QFile fileName("C:/0data/QT Projects new/datafile");
      fileName.open(QIODevice::ReadOnly);

      QDataStream in(&fileName);

      QByteArray record_data(1,0);

      while (!in.atEnd()) {

          in.readRawData(record_data.data(),1);
          v_lap=(*record_data.constData());
      
          in.readRawData(record_data.data(),1);
          v_data1=(*record_data.constData());
      
          in.readRawData(record_data.data(),1);
          v_data2=(*record_data.constData());
      
          in.readRawData(record_data.data(),1);
          v_data3=(*record_data.constData());
      
          in.readRawData(record_data.data(),1);
          v_data4=(*record_data.constData());
      

      }

      and the result in the debugger is:

      		v_data1	48	QVariant (int)
      		v_data2	49	QVariant (int)
      		v_data3	50	QVariant (int)
      		v_data4	51	QVariant (int)
      

      Thks to all of you for the support and sorry to not catch up so quick

      JP

      1 Reply Last reply
      0
      • jipe3001J jipe3001

        Hi guys, I am trying to read a raw data file as follows:

         QFile fileName("C:/0data/QT Projects new/falcon/18071209.422");
        fileName.open(QIODevice::ReadOnly);
         QDataStream in(&fileName);
        
        QByteArray line_data(7,0);
        
        while (!in.atEnd()) {
        
         in.readRawData(line_data.data(),7);
        
                   v_lap     = line_data.mid(0,1);
                   v_data1   = line_data.mid(1,1);
                   v_data2   = line_data.mid(2,1);
                   v_data3   = line_data.mid(3,1);
                   v_data4   = line_data.mid(4,1);
                   v_datacr  = line_data.mid(5,1);
                   v_datalf  = line_data.mid(6,1);
        
                   vlap_max = v_lap.toInt();
        }
        

        And here are the results:

        Variables locales		
        	line_data	"\001\030\030\000\030\r\n"	QByteArray
        		v_data1	"\030"	QVariant (QByteArray)
        		v_data2	"\030"	QVariant (QByteArray)
        		v_data3	"\000"	QVariant (QByteArray)
        		v_data4	"\030"	QVariant (QByteArray)
        		v_datacr	"\r"	QString
        		v_datalf	"\n"	QString
        		v_lap	"\001"	QVariant (QByteArray)
        		vlap_max	0	qint16
        

        My concern is about the values who seems to be always seen as char despite a raw_data read and
        when trying to convert v_lap to int the result is always 0 !!

        Thks for your usual coop

        JP

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @jipe3001 See http://doc.qt.io/qt-5/qbytearray.html#toInt
        You should rather use QDataStream and do the conversion using its >> operators like http://doc.qt.io/qt-5/qdatastream.html#operator-gt-gt-4

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

        1 Reply Last reply
        0
        • jipe3001J jipe3001

          Hi guys, I am trying to read a raw data file as follows:

           QFile fileName("C:/0data/QT Projects new/falcon/18071209.422");
          fileName.open(QIODevice::ReadOnly);
           QDataStream in(&fileName);
          
          QByteArray line_data(7,0);
          
          while (!in.atEnd()) {
          
           in.readRawData(line_data.data(),7);
          
                     v_lap     = line_data.mid(0,1);
                     v_data1   = line_data.mid(1,1);
                     v_data2   = line_data.mid(2,1);
                     v_data3   = line_data.mid(3,1);
                     v_data4   = line_data.mid(4,1);
                     v_datacr  = line_data.mid(5,1);
                     v_datalf  = line_data.mid(6,1);
          
                     vlap_max = v_lap.toInt();
          }
          

          And here are the results:

          Variables locales		
          	line_data	"\001\030\030\000\030\r\n"	QByteArray
          		v_data1	"\030"	QVariant (QByteArray)
          		v_data2	"\030"	QVariant (QByteArray)
          		v_data3	"\000"	QVariant (QByteArray)
          		v_data4	"\030"	QVariant (QByteArray)
          		v_datacr	"\r"	QString
          		v_datalf	"\n"	QString
          		v_lap	"\001"	QVariant (QByteArray)
          		vlap_max	0	qint16
          

          My concern is about the values who seems to be always seen as char despite a raw_data read and
          when trying to convert v_lap to int the result is always 0 !!

          Thks for your usual coop

          JP

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #3

          @jipe3001 said in Reading Raw Data from File:

          thats because v_lap is a QVariant of type (QByteArray) theres most likly no conversion in QVariant that turns a QByteArray into a n int

          try the following

          v_lap     = line_data.mid(0,1).toInt();
          

          thats still with some heavy copy and conversion operation but it should return the correct int.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #4
            1. Why are you using QVariant?
            2. QVariant::toInt on a QByteArray will use string conversion

            My concern is about the values who seems to be always seen as char

            char is the only type in the standard assured to have sizeof==1 so it is used to represent a "byte of data"

            when trying to convert v_lap to int the result is always 0

            What you want is int v_lapInt(*v_lap.toByteArray().constData());

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            4
            • jipe3001J Offline
              jipe3001J Offline
              jipe3001
              wrote on last edited by
              #5

              Thks for all your answers but I guess my real problem is about the conversion to INT

              For example I am reading the following QByteArray as follows:

              QByteArray test_lap(1,0);
              in.readRawData(test_lap.data(),0);

              And I've got this in debuging mode monitor:

              test_lap "\001" QByteArray
              1 0x01 char

              Therefore I tried to convert this Hexa value to Int as follows:

              bool ok;
              int x_lap2 = test_lap.toInt(&ok,16);
              

              But the results are always:

              ok false bool
              x_lap2 0 int

              What is wrong is this convertion which should be so simple?

              jsulmJ M VRoninV 3 Replies Last reply
              0
              • jipe3001J jipe3001

                Thks for all your answers but I guess my real problem is about the conversion to INT

                For example I am reading the following QByteArray as follows:

                QByteArray test_lap(1,0);
                in.readRawData(test_lap.data(),0);

                And I've got this in debuging mode monitor:

                test_lap "\001" QByteArray
                1 0x01 char

                Therefore I tried to convert this Hexa value to Int as follows:

                bool ok;
                int x_lap2 = test_lap.toInt(&ok,16);
                

                But the results are always:

                ok false bool
                x_lap2 0 int

                What is wrong is this convertion which should be so simple?

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @jipe3001 You should really take a look at http://doc.qt.io/qt-5/qdatastream.html It greatly simplifies reading binary data.

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

                1 Reply Last reply
                2
                • jipe3001J jipe3001

                  Thks for all your answers but I guess my real problem is about the conversion to INT

                  For example I am reading the following QByteArray as follows:

                  QByteArray test_lap(1,0);
                  in.readRawData(test_lap.data(),0);

                  And I've got this in debuging mode monitor:

                  test_lap "\001" QByteArray
                  1 0x01 char

                  Therefore I tried to convert this Hexa value to Int as follows:

                  bool ok;
                  int x_lap2 = test_lap.toInt(&ok,16);
                  

                  But the results are always:

                  ok false bool
                  x_lap2 0 int

                  What is wrong is this convertion which should be so simple?

                  M Offline
                  M Offline
                  mpergand
                  wrote on last edited by mpergand
                  #7

                  @jipe3001 said in Reading Raw Data from File:

                  test_lap "\001" QByteArray
                  1 0x01 char

                  It's not a ascii hex value,.

                  example:

                  const char* c="d";   // 13 hex
                  QByteArray arr= QByteArray(c);
                  int v=arr.toInt(0,16); // convert to binary
                   qDebug()<<v; // display 13 in decimal (default)
                  

                  \001 means 1 ( escape octal value)

                  const char* c="\011";   // 9 octal
                  QByteArray arr= QByteArray(c);
                  int v=arr.at(0); 
                  qDebug()<<v; // display 9 in decimal (default)
                  
                  1 Reply Last reply
                  0
                  • jipe3001J jipe3001

                    Thks for all your answers but I guess my real problem is about the conversion to INT

                    For example I am reading the following QByteArray as follows:

                    QByteArray test_lap(1,0);
                    in.readRawData(test_lap.data(),0);

                    And I've got this in debuging mode monitor:

                    test_lap "\001" QByteArray
                    1 0x01 char

                    Therefore I tried to convert this Hexa value to Int as follows:

                    bool ok;
                    int x_lap2 = test_lap.toInt(&ok,16);
                    

                    But the results are always:

                    ok false bool
                    x_lap2 0 int

                    What is wrong is this convertion which should be so simple?

                    VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by VRonin
                    #8

                    @jipe3001 said in Reading Raw Data from File:

                    What is wrong is this convertion which should be so simple?

                    @VRonin said in Reading Raw Data from File:

                    toInt on a QByteArray will use string conversion
                    What you want is int v_lapInt(*v_lap.toByteArray().constData());

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    1 Reply Last reply
                    1
                    • jipe3001J Offline
                      jipe3001J Offline
                      jipe3001
                      wrote on last edited by
                      #9

                      I finally succeed to read my file in Raw mode with QVariant as follows:

                      QFile fileName("C:/0data/QT Projects new/datafile");
                      fileName.open(QIODevice::ReadOnly);

                      QDataStream in(&fileName);

                      QByteArray record_data(1,0);

                      while (!in.atEnd()) {

                          in.readRawData(record_data.data(),1);
                          v_lap=(*record_data.constData());
                      
                          in.readRawData(record_data.data(),1);
                          v_data1=(*record_data.constData());
                      
                          in.readRawData(record_data.data(),1);
                          v_data2=(*record_data.constData());
                      
                          in.readRawData(record_data.data(),1);
                          v_data3=(*record_data.constData());
                      
                          in.readRawData(record_data.data(),1);
                          v_data4=(*record_data.constData());
                      

                      }

                      and the result in the debugger is:

                      		v_data1	48	QVariant (int)
                      		v_data2	49	QVariant (int)
                      		v_data3	50	QVariant (int)
                      		v_data4	51	QVariant (int)
                      

                      Thks to all of you for the support and sorry to not catch up so quick

                      JP

                      1 Reply Last reply
                      0

                      • Login

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