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.2k 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 Offline
    V Offline
    vicksoccer
    wrote on 15 Dec 2018, 09:47 last edited by
    #1

    Hello,
    I have to read binary data from file and convert 4 byte of string data into float value.

    I have QString of data {0x005b, 0x0049, 0x00be, 0x2039}, first I converted this string into QBytearray and then convert array into float using little endianess, here my code is...

    QBytearray array = data.toLocal8Bit(); // I get array of {5b,49,be,8b}

    int intdata = qFromLittleEndian<int> (array);
    float floatdata = *reinterpret_cast<float *> (&intdata);

    here I am getting float value is -7.32957e-32 but I am expecting value of float is -0.196638
    If I change order of QBytearray like {8b,5b,49,be} then only I am getting my expected float value.
    But I have multiple 4 byte of string data and each time changing order of Qbytedata is not same, and sometime without changing order of QBytearray I am getting expected float value, so I need universal solution for this. How can I get expected float value.
    Example -
    array = {bd,bc,bd,c6} = Expected float value is -0.0918
    array = {d2,f1,3f,92} = Expected float value is 1.8892

    I have tried to do sorting of array also before conversion but does not help much.

    K A 2 Replies Last reply 15 Dec 2018, 11:05
    0
    • V vicksoccer
      15 Dec 2018, 09:47

      Hello,
      I have to read binary data from file and convert 4 byte of string data into float value.

      I have QString of data {0x005b, 0x0049, 0x00be, 0x2039}, first I converted this string into QBytearray and then convert array into float using little endianess, here my code is...

      QBytearray array = data.toLocal8Bit(); // I get array of {5b,49,be,8b}

      int intdata = qFromLittleEndian<int> (array);
      float floatdata = *reinterpret_cast<float *> (&intdata);

      here I am getting float value is -7.32957e-32 but I am expecting value of float is -0.196638
      If I change order of QBytearray like {8b,5b,49,be} then only I am getting my expected float value.
      But I have multiple 4 byte of string data and each time changing order of Qbytedata is not same, and sometime without changing order of QBytearray I am getting expected float value, so I need universal solution for this. How can I get expected float value.
      Example -
      array = {bd,bc,bd,c6} = Expected float value is -0.0918
      array = {d2,f1,3f,92} = Expected float value is 1.8892

      I have tried to do sorting of array also before conversion but does not help much.

      K Offline
      K Offline
      koahnig
      wrote on 15 Dec 2018, 11:05 last edited by
      #2

      @vicksoccer

      Hi and welcome to devnet forum

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

      I have QString of data {0x005b, 0x0049, 0x00be, 0x2039}, first I converted this string into QBytearray and then convert array into float using little endianess, here my code is...

      These are not 4 bytes. Even when assuming that each byte has a leading zero byte, which ,may be removed without change, it is not true. How do you store the two bytes represented by 0x2039?

      Vote the answer(s) that helped you to solve your issue(s)

      V 1 Reply Last reply 15 Dec 2018, 11:36
      2
      • V vicksoccer
        15 Dec 2018, 09:47

        Hello,
        I have to read binary data from file and convert 4 byte of string data into float value.

        I have QString of data {0x005b, 0x0049, 0x00be, 0x2039}, first I converted this string into QBytearray and then convert array into float using little endianess, here my code is...

        QBytearray array = data.toLocal8Bit(); // I get array of {5b,49,be,8b}

        int intdata = qFromLittleEndian<int> (array);
        float floatdata = *reinterpret_cast<float *> (&intdata);

        here I am getting float value is -7.32957e-32 but I am expecting value of float is -0.196638
        If I change order of QBytearray like {8b,5b,49,be} then only I am getting my expected float value.
        But I have multiple 4 byte of string data and each time changing order of Qbytedata is not same, and sometime without changing order of QBytearray I am getting expected float value, so I need universal solution for this. How can I get expected float value.
        Example -
        array = {bd,bc,bd,c6} = Expected float value is -0.0918
        array = {d2,f1,3f,92} = Expected float value is 1.8892

        I have tried to do sorting of array also before conversion but does not help much.

        A Offline
        A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on 15 Dec 2018, 11:23 last edited by
        #3

        @vicksoccer

        I'd agree to @koahnig that your data is messed up.

        Note that it is not possible to store binary data in a QString, while it is possible to do so in a QByteArray.

        So, please tell us, where is your data coming from? Are you sure the format is little endian?

        Qt has to stay free or it will die.

        1 Reply Last reply
        2
        • K koahnig
          15 Dec 2018, 11:05

          @vicksoccer

          Hi and welcome to devnet forum

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

          I have QString of data {0x005b, 0x0049, 0x00be, 0x2039}, first I converted this string into QBytearray and then convert array into float using little endianess, here my code is...

          These are not 4 bytes. Even when assuming that each byte has a leading zero byte, which ,may be removed without change, it is not true. How do you store the two bytes represented by 0x2039?

          V Offline
          V Offline
          vicksoccer
          wrote on 15 Dec 2018, 11:36 last edited by
          #4

          @koahnig I have QString of 4 Byte which includes Ascii, dec and hex representation, and here I am entering hex value only then I stored this string into array using local8Bit() function of qstring.
          here I am attached image of both data string and array -
          0_1544873107224_1.PNG 0_1544873129162_2.PNG

          here I am getting float value is -7.32957e-32 with above syntax but I am expecting value of float is -0.196638
          I have check this values on online converter also https://gregstoll.dyndns.org/~gregstoll/floattohex/
          ,here I am getting my expected float value after changing index order of array like {0x8b,0x5b,0x49,0xbe}. as I said I have multiple 4 byte of string data and each time changing index order is not same, and sometime without changing index order of QBytearray I am getting expected float value, so I need universal solution for this. I don't know how to set index order and on which condition array order will be set..I am struggling for that.

          A 1 Reply Last reply 15 Dec 2018, 11:41
          0
          • V vicksoccer
            15 Dec 2018, 11:36

            @koahnig I have QString of 4 Byte which includes Ascii, dec and hex representation, and here I am entering hex value only then I stored this string into array using local8Bit() function of qstring.
            here I am attached image of both data string and array -
            0_1544873107224_1.PNG 0_1544873129162_2.PNG

            here I am getting float value is -7.32957e-32 with above syntax but I am expecting value of float is -0.196638
            I have check this values on online converter also https://gregstoll.dyndns.org/~gregstoll/floattohex/
            ,here I am getting my expected float value after changing index order of array like {0x8b,0x5b,0x49,0xbe}. as I said I have multiple 4 byte of string data and each time changing index order is not same, and sometime without changing index order of QBytearray I am getting expected float value, so I need universal solution for this. I don't know how to set index order and on which condition array order will be set..I am struggling for that.

            A Offline
            A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on 15 Dec 2018, 11:41 last edited by
            #5

            @vicksoccer

            As said before, it is not possible to store binary data in a QString, while it is possible to do so in a QByteArray.

            Qt has to stay free or it will die.

            V 1 Reply Last reply 15 Dec 2018, 12:03
            2
            • A aha_1980
              15 Dec 2018, 11:41

              @vicksoccer

              As said before, it is not possible to store binary data in a QString, while it is possible to do so in a QByteArray.

              V Offline
              V Offline
              vicksoccer
              wrote on 15 Dec 2018, 12:03 last edited by
              #6

              @aha_1980 Thanks for your reply here I am trying to decode .slc file, sorry I am little bit confused,Its not exaclty binary file, but includes number of floating values...

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

              I am reading all data of file and stored into string.. then trying to decode this . I have decode half file as per this above syntax successfully .. string data is little endian/Big endian scheme.

              K S jsulmJ 3 Replies Last reply 15 Dec 2018, 12:22
              0
              • V vicksoccer
                15 Dec 2018, 12:03

                @aha_1980 Thanks for your reply here I am trying to decode .slc file, sorry I am little bit confused,Its not exaclty binary file, but includes number of floating values...

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

                I am reading all data of file and stored into string.. then trying to decode this . I have decode half file as per this above syntax successfully .. string data is little endian/Big endian scheme.

                K Offline
                K Offline
                koahnig
                wrote on 15 Dec 2018, 12:22 last edited by koahnig
                #7

                @vicksoccer

                See http://doc.qt.io/qt-5/qstring.html#details you will find this text

                *The QString class provides a Unicode character string.

                QString stores a string of 16-bit QChars, where each QChar corresponds to one UTF-16 code unit. (Unicode characters with code values above 65535 are stored using surrogate pairs, i.e., two consecutive QChars.)*

                You would need to read byte by byte from QDatastream. Therefore, you need to follow @aha_1980's advice.

                Vote the answer(s) that helped you to solve your issue(s)

                1 Reply Last reply
                2
                • V vicksoccer
                  15 Dec 2018, 12:03

                  @aha_1980 Thanks for your reply here I am trying to decode .slc file, sorry I am little bit confused,Its not exaclty binary file, but includes number of floating values...

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

                  I am reading all data of file and stored into string.. then trying to decode this . I have decode half file as per this above syntax successfully .. string data is little endian/Big endian scheme.

                  S Offline
                  S Offline
                  Stoyan
                  wrote on 15 Dec 2018, 13:05 last edited by
                  #8

                  @vicksoccer
                  What is exact format of these floating values in the file?
                  If they are in binary format: "5B 49 BE 8B", then you have to use QDataStream instead QTextStream and store data in QByteArray instead QString. Or you can read data directly from the file into float:

                  QFile slcFile(fileName);
                  slcFile.open(QIODevice::ReadOnly);
                  QDataStream in(&slcFile);
                  in.setByteOrder(QDataStream::LittleEndian);
                  in.setFloatingPointPrecision(QDataStream::SinglePrecision);
                  float f;
                  in >> f;
                  ...
                  
                  1 Reply Last reply
                  4
                  • V vicksoccer
                    15 Dec 2018, 12:03

                    @aha_1980 Thanks for your reply here I am trying to decode .slc file, sorry I am little bit confused,Its not exaclty binary file, but includes number of floating values...

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

                    I am reading all data of file and stored into string.. then trying to decode this . I have decode half file as per this above syntax successfully .. string data is little endian/Big endian scheme.

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 17 Dec 2018, 07:55 last edited by
                    #9

                    @vicksoccer You should rather use http://doc.qt.io/qt-5/qdatastream.html to read/write binary data.

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

                    1 Reply Last reply
                    3
                    • V Offline
                      V Offline
                      vicksoccer
                      wrote on 18 Dec 2018, 07:31 last edited by
                      #10

                      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...

                      jsulmJ A 2 Replies Last reply 18 Dec 2018, 07:48
                      0
                      • 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...

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 18 Dec 2018, 07:48 last edited by
                        #11

                        @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

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

                        V 1 Reply Last reply 18 Dec 2018, 08:28
                        1
                        • 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
                          • jsulmJ 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

                            jsulmJ 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

                              jsulmJ Offline
                              jsulmJ Offline
                              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
                              • jsulmJ 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 ..

                                jsulmJ 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 ..

                                  jsulmJ Offline
                                  jsulmJ Offline
                                  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
                                  • jsulmJ 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..

                                    jsulmJ kshegunovK 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..

                                      jsulmJ Offline
                                      jsulmJ Offline
                                      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
                                      • jsulmJ 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

                                          1/31

                                          15 Dec 2018, 09:47

                                          • Login

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