Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Converting 4 Byte array into float using little endianness
Forum Updated to NodeBB v4.3 + New Features

Converting 4 Byte array into float using little endianness

Scheduled Pinned Locked Moved Solved Mobile and Embedded
31 Posts 6 Posters 8.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.
  • V vicksoccer
    18 Dec 2018, 07:31

    Thanks all for your kind support!!!
    Actually I am reading file which includes AScii characters. I have read all data from file and stored into QString.

    QFile slcFile(fileName); // filename is path of file.
    QTextStream in(&slcFile);
    QString slcText = in.readAll();

    yes I understood I can store data as QBytearray also. After storing I have process string data, But while reading data slcText does not include "\n" after end of line in QFIle. If I had open original file with texteditor (notepad++) then I was able see location of end of line .. but for many end lines slcText does not include "\n" and I need resulted string with "\n" to process. It happens same with QBytearray. If I get reading all data with "\n" my problem will be solved. Any suggestions for this...

    A Offline
    A Offline
    aha_1980
    Lifetime Qt Champion
    wrote on 18 Dec 2018, 08:21 last edited by
    #12

    @vicksoccer If your file is mixed text/binary, you should read it as binary and process the data yourself.

    Once it is converted to text only (QString), important information will be lost. So read as binary, and convert only the chunks that are known as text to QString.

    Regards

    Qt has to stay free or it will die.

    1 Reply Last reply
    1
    • J jsulm
      18 Dec 2018, 07:48

      @vicksoccer said in Converting 4 Byte array into float using little endianness:

      Actually I am reading file which includes AScii characters

      Well, in your first post you wrote "I have to read binary data from file". This is confusing at least.
      So, lets make it clear: you are reading a text file - is that correct?
      You can read the file without QTextStream like shown here: http://doc.qt.io/qt-5/qfile.html

      V Offline
      V Offline
      vicksoccer
      wrote on 18 Dec 2018, 08:28 last edited by
      #13

      @jsulm Sorry for the last post but yes I am reading Text file and its missing "\n" character at the end of line in resulted string data..thats the issue now

      J 1 Reply Last reply 18 Dec 2018, 08:29
      0
      • V vicksoccer
        18 Dec 2018, 08:28

        @jsulm Sorry for the last post but yes I am reading Text file and its missing "\n" character at the end of line in resulted string data..thats the issue now

        J Online
        J Online
        jsulm
        Lifetime Qt Champion
        wrote on 18 Dec 2018, 08:29 last edited by
        #14

        @vicksoccer Check the link I posted...

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

        V 1 Reply Last reply 18 Dec 2018, 08:38
        0
        • J jsulm
          18 Dec 2018, 08:29

          @vicksoccer Check the link I posted...

          V Offline
          V Offline
          vicksoccer
          wrote on 18 Dec 2018, 08:38 last edited by
          #15

          @jsulm I tried both using stream and without stream also but still getting same issue. is there any other way to read file data with endline as QString or QBytearray ..

          J 1 Reply Last reply 18 Dec 2018, 08:41
          0
          • V vicksoccer
            18 Dec 2018, 08:38

            @jsulm I tried both using stream and without stream also but still getting same issue. is there any other way to read file data with endline as QString or QBytearray ..

            J Online
            J Online
            jsulm
            Lifetime Qt Champion
            wrote on 18 Dec 2018, 08:41 last edited by jsulm
            #16

            @vicksoccer If the file really contains \n then you will get them when reading. How do you know it does not read them?
            Also if you want read line by line use readLine().

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

            V 1 Reply Last reply 18 Dec 2018, 08:49
            0
            • J jsulm
              18 Dec 2018, 08:41

              @vicksoccer If the file really contains \n then you will get them when reading. How do you know it does not read them?
              Also if you want read line by line use readLine().

              V Offline
              V Offline
              vicksoccer
              wrote on 18 Dec 2018, 08:49 last edited by vicksoccer
              #17

              @jsulm I have opened original file with texteditor(notepad++) there I can see data line by line..
              But after reading file with above qt syntax the resulted QString or Qbytearray does not contain "\n" character at end of the line.
              I have tried with readline() also but I am getting truncated string at wrong location ideally it comes at end line..
              here my data is (opened in notpad++)
              0_1545123421025_3.PNG
              this is what I am getting in QString after readall() 0_1545123560600_4.PNG
              here I am expecting "\n" character after index of 540..

              J K 2 Replies Last reply 18 Dec 2018, 09:19
              0
              • V vicksoccer
                18 Dec 2018, 08:49

                @jsulm I have opened original file with texteditor(notepad++) there I can see data line by line..
                But after reading file with above qt syntax the resulted QString or Qbytearray does not contain "\n" character at end of the line.
                I have tried with readline() also but I am getting truncated string at wrong location ideally it comes at end line..
                here my data is (opened in notpad++)
                0_1545123421025_3.PNG
                this is what I am getting in QString after readall() 0_1545123560600_4.PNG
                here I am expecting "\n" character after index of 540..

                J Online
                J Online
                jsulm
                Lifetime Qt Champion
                wrote on 18 Dec 2018, 09:19 last edited by
                #18

                @vicksoccer Can you show exact code you use to read the file?

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

                V 1 Reply Last reply 18 Dec 2018, 09:28
                0
                • J jsulm
                  18 Dec 2018, 09:19

                  @vicksoccer Can you show exact code you use to read the file?

                  V Offline
                  V Offline
                  vicksoccer
                  wrote on 18 Dec 2018, 09:28 last edited by
                  #19

                  @jsulm here my code is for reading file

                  QFile slcFile(fileName); //path of the file
                  if(!slcFile.open(QIODevice::ReadOnly | QIODevice::Text))
                  {
                  showStatus("Unable to open the file\n"); //MessageBOx
                  return;
                  }
                  QTextStream in(&slcFile);
                  QString slcText = in.readAll(); //Reading data as string

                  A 1 Reply Last reply 18 Dec 2018, 09:38
                  0
                  • V vicksoccer
                    18 Dec 2018, 09:28

                    @jsulm here my code is for reading file

                    QFile slcFile(fileName); //path of the file
                    if(!slcFile.open(QIODevice::ReadOnly | QIODevice::Text))
                    {
                    showStatus("Unable to open the file\n"); //MessageBOx
                    return;
                    }
                    QTextStream in(&slcFile);
                    QString slcText = in.readAll(); //Reading data as string

                    A Offline
                    A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on 18 Dec 2018, 09:38 last edited by
                    #20

                    @vicksoccer And do you have an example for such a file?

                    Also, wasn't you question about binary float representation? How does that fit with a text file?

                    Qt has to stay free or it will die.

                    V 1 Reply Last reply 18 Dec 2018, 09:55
                    0
                    • V vicksoccer
                      18 Dec 2018, 08:49

                      @jsulm I have opened original file with texteditor(notepad++) there I can see data line by line..
                      But after reading file with above qt syntax the resulted QString or Qbytearray does not contain "\n" character at end of the line.
                      I have tried with readline() also but I am getting truncated string at wrong location ideally it comes at end line..
                      here my data is (opened in notpad++)
                      0_1545123421025_3.PNG
                      this is what I am getting in QString after readall() 0_1545123560600_4.PNG
                      here I am expecting "\n" character after index of 540..

                      K Offline
                      K Offline
                      kshegunov
                      Moderators
                      wrote on 18 Dec 2018, 09:54 last edited by kshegunov
                      #21

                      @vicksoccer said in Converting 4 Byte array into float using little endianness:

                      here I am expecting "\n" character after index of 540

                      Why? From what I could see from your screenshots this is a binary file. The fact that you can see the text in it in your notepad++ doesn't make it a text file. If you don't trust me open an executable in notepad++ and you're going to see the static strings from your program (in one way or another).

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      4
                      • A aha_1980
                        18 Dec 2018, 09:38

                        @vicksoccer And do you have an example for such a file?

                        Also, wasn't you question about binary float representation? How does that fit with a text file?

                        V Offline
                        V Offline
                        vicksoccer
                        wrote on 18 Dec 2018, 09:55 last edited by
                        #22

                        @aha_1980 Previously I was stucked at conversion of 4 QBytearray into float thats why I am posted with this..I am truely new in
                        this but now I am getting to know that my conversion is messed up due to skiping "\n" character at end line in receiving data as QString.
                        here is sample .slc file
                        https://www.dropbox.com/s/5bez93mdjr64v8x/open_display_SLC.zip

                        A 1 Reply Last reply 18 Dec 2018, 10:24
                        0
                        • V vicksoccer
                          18 Dec 2018, 09:55

                          @aha_1980 Previously I was stucked at conversion of 4 QBytearray into float thats why I am posted with this..I am truely new in
                          this but now I am getting to know that my conversion is messed up due to skiping "\n" character at end line in receiving data as QString.
                          here is sample .slc file
                          https://www.dropbox.com/s/5bez93mdjr64v8x/open_display_SLC.zip

                          A Offline
                          A Offline
                          aha_1980
                          Lifetime Qt Champion
                          wrote on 18 Dec 2018, 10:24 last edited by
                          #23

                          @vicksoccer By looking at your sample_ring2.slc, I doubt that is a text file. It looks like pure binary data for me.

                          Qt has to stay free or it will die.

                          V 2 Replies Last reply 18 Dec 2018, 10:40
                          1
                          • A aha_1980
                            18 Dec 2018, 10:24

                            @vicksoccer By looking at your sample_ring2.slc, I doubt that is a text file. It looks like pure binary data for me.

                            V Offline
                            V Offline
                            vicksoccer
                            wrote on 18 Dec 2018, 10:40 last edited by
                            #24

                            @aha_1980 I have tried to read this file with

                            @
                            QFile slcFile(fileName); //path of the file
                            if(!slcFile.open(QIODevice::ReadOnly | QIODevice::Text))
                            {
                            showStatus("Unable to open the file\n"); //MessageBOx
                            return;
                            }
                            QByteArray slcText = slcFile.readAll();
                            @
                            But still missing "\n" in QBytearray in required index.
                            I have take 4 data from QBytearray and convert into float value in loop so due missing "\n" index my conversion is messed up after performing some loop and does not getting expected float value.

                            K 1 Reply Last reply 18 Dec 2018, 10:41
                            0
                            • V vicksoccer
                              18 Dec 2018, 10:40

                              @aha_1980 I have tried to read this file with

                              @
                              QFile slcFile(fileName); //path of the file
                              if(!slcFile.open(QIODevice::ReadOnly | QIODevice::Text))
                              {
                              showStatus("Unable to open the file\n"); //MessageBOx
                              return;
                              }
                              QByteArray slcText = slcFile.readAll();
                              @
                              But still missing "\n" in QBytearray in required index.
                              I have take 4 data from QBytearray and convert into float value in loop so due missing "\n" index my conversion is messed up after performing some loop and does not getting expected float value.

                              K Offline
                              K Offline
                              kshegunov
                              Moderators
                              wrote on 18 Dec 2018, 10:41 last edited by
                              #25

                              @vicksoccer said in Converting 4 Byte array into float using little endianness:

                              But still missing "\n" in QBytearray in required index.

                              There's no such requirement when working with binary files. This is relevant only for text files, which you don't have in this case.

                              Read and abide by the Qt Code of Conduct

                              V 1 Reply Last reply 18 Dec 2018, 11:02
                              1
                              • K kshegunov
                                18 Dec 2018, 10:41

                                @vicksoccer said in Converting 4 Byte array into float using little endianness:

                                But still missing "\n" in QBytearray in required index.

                                There's no such requirement when working with binary files. This is relevant only for text files, which you don't have in this case.

                                V Offline
                                V Offline
                                vicksoccer
                                wrote on 18 Dec 2018, 11:02 last edited by
                                #26

                                @kshegunov Thanks for your reply..
                                Then how I can read this file can you just show me bunch of codes.. that will be nice

                                K 1 Reply Last reply 18 Dec 2018, 11:08
                                0
                                • V vicksoccer
                                  18 Dec 2018, 11:02

                                  @kshegunov Thanks for your reply..
                                  Then how I can read this file can you just show me bunch of codes.. that will be nice

                                  K Offline
                                  K Offline
                                  kshegunov
                                  Moderators
                                  wrote on 18 Dec 2018, 11:08 last edited by
                                  #27

                                  @vicksoccer said in Converting 4 Byte array into float using little endianness:

                                  can you just show me bunch of codes

                                  I can't because each binary file has its own format and is different in that regard. You should read on the file format's specification to understand what is written how before proceeding with code.

                                  Read and abide by the Qt Code of Conduct

                                  1 Reply Last reply
                                  1
                                  • A aha_1980
                                    18 Dec 2018, 10:24

                                    @vicksoccer By looking at your sample_ring2.slc, I doubt that is a text file. It looks like pure binary data for me.

                                    V Offline
                                    V Offline
                                    vicksoccer
                                    wrote on 18 Dec 2018, 11:36 last edited by
                                    #28

                                    @aha_1980 @Qt-Champions-2016 @Moderators
                                    Please anyone can help me to read this sample_ring2.slc file I am trying to decode this slc file in qt

                                    J S 2 Replies Last reply 18 Dec 2018, 11:40
                                    0
                                    • V vicksoccer
                                      18 Dec 2018, 11:36

                                      @aha_1980 @Qt-Champions-2016 @Moderators
                                      Please anyone can help me to read this sample_ring2.slc file I am trying to decode this slc file in qt

                                      J Online
                                      J Online
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on 18 Dec 2018, 11:40 last edited by
                                      #29

                                      @vicksoccer Please do not contact directly Qt-Champions and Moderators! If somebody has an answer he/she will write it. People here help using their own time.
                                      Do you have a specification for this file format as @kshegunov already asked?

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

                                      1 Reply Last reply
                                      3
                                      • V vicksoccer
                                        18 Dec 2018, 11:36

                                        @aha_1980 @Qt-Champions-2016 @Moderators
                                        Please anyone can help me to read this sample_ring2.slc file I am trying to decode this slc file in qt

                                        S Offline
                                        S Offline
                                        Stoyan
                                        wrote on 18 Dec 2018, 18:38 last edited by Stoyan
                                        #30

                                        @vicksoccer
                                        I saw that this SLC-file is created by JewelCAD. With an easy search I found SLC file format description.
                                        If you want to read just header of the file it is a lot easier - use QIODevice::readLine to read the header in QByteArray. Then extract the part after "-EXTENTS", remove spaces, use split(',') or parse array manually to extract every number and convert it to float with toFloat().
                                        If you want to read and decode entire file, you have to study the whole description.

                                        1 Reply Last reply
                                        2
                                        • V Offline
                                          V Offline
                                          vicksoccer
                                          wrote on 19 Dec 2018, 07:13 last edited by
                                          #31

                                          Thank you all for support!!!
                                          This was first and nice experience to post on forum..Sorry for all above confusion..
                                          Finally my issue get solved,
                                          @
                                          QFile slcFile(fileName); //path of the file
                                          if(!slcFile.open(QIODevice::ReadOnly))
                                          {
                                          showStatus("Unable to open the file\n"); //MessageBOx
                                          return;
                                          }
                                          QString slcText = slcFile.readAll();

                                          here I only removed QIODevice::Text in file opening, now I am getting "\r" character at required location in resulted QString, So after conversion in loop getting expected float values..

                                          1 Reply Last reply
                                          1

                                          21/31

                                          18 Dec 2018, 09:54

                                          • Login

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