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. Read text file with hex data
QtWS25 Last Chance

Read text file with hex data

Scheduled Pinned Locked Moved General and Desktop
11 Posts 4 Posters 11.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.
  • J Offline
    J Offline
    Jayson.Prichard
    wrote on 7 Nov 2013, 02:02 last edited by
    #1

    I have a text file. The first 4 lines contains text. The next n lines contains a 50 character line that is hex data. I have no problem reading the text lines, but have no idea how to read the hex values.

    I need to read the lines that contain hex values one word at a time and then byte swap. I am opening the file using a QTextStream. I'm stumped. Any ideas

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Code_ReaQtor
      wrote on 7 Nov 2013, 02:16 last edited by
      #2

      Try QDataStream, instead.

      Please visit my open-source projects at https://github.com/Code-ReaQtor.

      1 Reply Last reply
      0
      • J Offline
        J Offline
        Jayson.Prichard
        wrote on 8 Nov 2013, 01:17 last edited by
        #3

        I couldn't get QDataStream to read the text lines in the file. Never got to test if it would read the remaining strings that represent hex data.

        Decided to just parse out 8 characters into a temp string and convert that to an unsigned short. Then I can byte swap that.

        I have it parsing out the 8 characters that represent a word. So now how do I convert a QString to an unsigned short? qstring::toUShort is giving me an error: "No matching function for call to 'QString::toUShortbool &int)'".

        The data is a string that represents hex data (this is all in a text file):
        0230313330383037330000000025352323392d323031380034

        The code to parse out a word is:
        @
        int startPos= 2;
        int endPos = 8;
        bool ok;
        QString tempStr;
        bool done = false;

        // While not end of file
        while (!CIUPreviousBITLogStream.atEnd())
        {
            str = CIUPreviousBITLogStream.readLine();
            if ( !str.isEmpty())
            {
                done = false;
            }
            else
            {
                done = true;
            }
            // While not end of line
            while (!done)
            {
                // Clear the temporary string variable.
                tempStr = "";
                // Parse out the next word.
                tempStr = str.mid(startPos, endPos);
                // Convert to unsigned short
                ushort TempWord = tempStr.toUShort(ok,10);
                // Byte swap
                //??
                if ( startPos >= str.length())
                {
                    done = true;
                }
                else
                {
                    startPos = (startPos + 8);
                    endPos   = (endPos);
                }
            }
        }@
        

        What's wrong with this conversion from QString to unsigned short?

        1 Reply Last reply
        0
        • J Offline
          J Offline
          JKSH
          Moderators
          wrote on 8 Nov 2013, 02:52 last edited by
          #4

          Hi,

          You don't need to write your own parser, just use "QByteArray::fromHex()":http://qt-project.org/doc/qt-5.1/qtcore/qbytearray.html#fromHex to convert hex characters to text.

          I find it easy to read straight from a QFile:

          @
          QFile file("text.txt");
          file.open(QFile::ReadOnly|QFile::Text);

          QString plainText = file.readLine().trimmed();
          QString hexText = QByteArray::fromHex(file.readLine().trimmed());

          file.close();
          @

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dbzhang800
            wrote on 8 Nov 2013, 03:05 last edited by
            #5

            Hi, IMO, nether QTextStream nor QDataStream is suitable for you. You can use QFile directly.

            1 Reply Last reply
            0
            • J Offline
              J Offline
              Jayson.Prichard
              wrote on 13 Nov 2013, 01:56 last edited by
              #6

              JKSH, Thanks. That looks like it will work; with one minor problem. The hex data contains instances of "00" (see my original post with data). QByteArray interprets this as an end of line and terminates the line at the first instance of "00".

              This code:
              @
              HexBA = QByteArray::fromHex(file.readLine());
              outTempFile << HexBA << endl;
              @
              Yeilds: "01308073"

              How can I tell QByteArray that "00" is not an end of line?

              1 Reply Last reply
              0
              • J Offline
                J Offline
                JKSH
                Moderators
                wrote on 13 Nov 2013, 02:48 last edited by
                #7

                [quote]
                @
                outTempFile << HexBA << endl;
                @
                [/quote]It looks like you're writing to outTempFile using a text stream.

                A QByteArray does not interpret 0x00 as EOL. -A text stream does.-

                EDIT: Actually, I just tested this and discovered this isn't true. If a QTextStream is fed raw bytes, it will not treat 0x00 as EOL.

                [quote]How can I tell QByteArray that “00” is not an end of line?[/quote]

                • If you want to store raw bytes, use a QDataStream or QFile::write().
                • If you want to store data in your original format, convert your bytes back to hex-encoded text and then use the text stream.

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  Jayson.Prichard
                  wrote on 13 Nov 2013, 03:41 last edited by
                  #8

                  I'm still learning Qt. I commented the textstream definition and tried the following, but got the same results:

                  @

                  // Define the name of the temporary file we'll use to create the report with.
                  QString tempFileName = "tempFile.txt";
                  QFile tempFile&#40;tempFileName&#41;;
                  tempFile.open(QIODevice::WriteOnly&#41;;
                  //QTextStream outTempFile&#40;&tempFile&#41;;
                  

                  .
                  .
                  .
                  HexBA = QByteArray::fromHex(file.readLine());
                  //outTempFile << HexBA << endl;
                  tempFile.write(HexBA);
                  @

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    JKSH
                    Moderators
                    wrote on 13 Nov 2013, 04:41 last edited by
                    #9

                    [quote]I’m still learning Qt.[/quote]All the best!

                    A "text stream":http://www.ibm.com/developerworks/library/l-lpic1-v3-103-2/ is a general computing concept -- it exists outside of Qt and outside of C++.

                    But anyway, I just tested this. It turns out that a QTextStream won't truncate at 0x00, but a QString will. Sorry for the wrong info.

                    Is HexBA a QString or QByteArray?

                    Also, how do you want to store your data? In binary (raw byte) format, or hex-text (string) format?

                    The string "0123" is represented by the bytes {0x30, 0x31, 0x32, 0x33}. This is not the same as {0x01, 0x23].

                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      Jayson.Prichard
                      wrote on 13 Nov 2013, 10:32 last edited by
                      #10

                      HexBA is a QByteArray. My plan is to store the data as a string/text.

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        JKSH
                        Moderators
                        wrote on 13 Nov 2013, 10:40 last edited by
                        #11

                        [quote author="passjj30169" date="1384338771"]HexBA is a QByteArray. My plan is to store the data as a string/text. [/quote]Then you will need to convert the byte data into string format before writing it.

                        Search the QByteArray documentation. You used fromHex() to convert hex-text to bytes. What can you use to convert bytes back into hex-text?

                        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                        1 Reply Last reply
                        0

                        9/11

                        13 Nov 2013, 04:41

                        • Login

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