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. About the missing byte when using Qfile read a binary file
Forum Updated to NodeBB v4.3 + New Features

About the missing byte when using Qfile read a binary file

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 2.1k 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.
  • M Offline
    M Offline
    MartinChan
    wrote on 14 Jul 2017, 08:33 last edited by
    #1

    I am using QFile to read a .cli file(which is used for 3D printing and),it is stored in binary order.(The byteOrder is least significant)

    Something wrong when I try to analyse the data,I used QFile getChar() function to get each data,but it missed a byte(0x0d, I know it is carriage return in ASCII but I am doing binary file reading so it should do nothing with ASCII) when I checked the result.

    Following is what I see in UltraEdit,it is clear that 0120h starts with C6-0A-0D-2B……However when I try to use QDebug to output each characteristic,the 0D disappeares.
    alt text

    To make sure whether it's due to getChar(),I tried QDataStream's readRawData() and QFile's read() ,but they also didn't work.

    I wrote a little program to test if 0x0d can't be read and the answer is yes.
    (I checked each byte in binary file,all byte value could be found except 00xd,which in decimal is 13).
    alt text

    Thank u all.

    R 1 Reply Last reply 14 Jul 2017, 08:40
    0
    • M MartinChan
      14 Jul 2017, 08:33

      I am using QFile to read a .cli file(which is used for 3D printing and),it is stored in binary order.(The byteOrder is least significant)

      Something wrong when I try to analyse the data,I used QFile getChar() function to get each data,but it missed a byte(0x0d, I know it is carriage return in ASCII but I am doing binary file reading so it should do nothing with ASCII) when I checked the result.

      Following is what I see in UltraEdit,it is clear that 0120h starts with C6-0A-0D-2B……However when I try to use QDebug to output each characteristic,the 0D disappeares.
      alt text

      To make sure whether it's due to getChar(),I tried QDataStream's readRawData() and QFile's read() ,but they also didn't work.

      I wrote a little program to test if 0x0d can't be read and the answer is yes.
      (I checked each byte in binary file,all byte value could be found except 00xd,which in decimal is 13).
      alt text

      Thank u all.

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 14 Jul 2017, 08:40 last edited by
      #2

      @MartinChan
      lets skip the guessing and show some code you are using.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MartinChan
        wrote on 14 Jul 2017, 08:52 last edited by
        #3

        Here it is and I omitted some details:

        //Read in binary file
        QFile file("e:/cli/6.cli");
        char *tmp=new char [1];
        
        if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
        {
        }else{
        while(!file.atEnd()){
                 //Read single byte each time
                    file.read(tmp,1);
                    int tmp_int=tmp[0]&(0xFF);/
        
                 //Output the result in hex 
                   QString key;
                    key =  QString("%1").arg(tmp_int,4,16,QLatin1Char('0'));
                    qDebug()<<key;
                   }
        }
        
        R 1 Reply Last reply 14 Jul 2017, 08:54
        0
        • M MartinChan
          14 Jul 2017, 08:52

          Here it is and I omitted some details:

          //Read in binary file
          QFile file("e:/cli/6.cli");
          char *tmp=new char [1];
          
          if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
          {
          }else{
          while(!file.atEnd()){
                   //Read single byte each time
                      file.read(tmp,1);
                      int tmp_int=tmp[0]&(0xFF);/
          
                   //Output the result in hex 
                     QString key;
                      key =  QString("%1").arg(tmp_int,4,16,QLatin1Char('0'));
                      qDebug()<<key;
                     }
          }
          
          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 14 Jul 2017, 08:54 last edited by
          #4

          @MartinChan said in About the missing byte when using Qfile read a binary file:

          remove QIODevice::Text flag from the file.open() call

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          5
          • M Offline
            M Offline
            MartinChan
            wrote on 14 Jul 2017, 09:04 last edited by
            #5

            It worked!Thx ~

            I read the open() doc and it says:

            • When reading, the end-of-line terminators are translated to '\n'. When writing, the end-of-line terminators are translated to the local encoding, for example '\r\n' for Win32.

            So it is due to the Qfile think 0x0D is '\r' when I falsely used it.

            1 Reply Last reply
            2

            1/5

            14 Jul 2017, 08:33

            • Login

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