Read text file with hex data



  • 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



  • Try QDataStream, instead.



  • 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?


  • Moderators

    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();
    @



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



  • 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?


  • Moderators

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


  • 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);
    @


  • Moderators

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



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


  • Moderators

    [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?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.