Solved Data Manipulation
-
It's been a while since I was doing some checksum work... Not really sure if I'm even close but no one else has said anything yet so... Is something like this any good for you?
int iFieldWidth = 2; int iNumberBase = 16; hexDataSum = QString("%1").arg(parsedValue, iFieldWidth, iNumberBase, QLatin1Char( '0' )).toUpper();
It's part of a pretty specific section of my message handling needs, but it might help?
-
@MScottM What is the type of DB1hex?
Also you should check ok after converting from hex string to integer. -
Hi,
there are a couple of ways to do what you want to do. I'll post a code snippet from one of my projects that should help you:
//for converting 2* 8bit to int QByteArray buffer; int value = static_cast<int>(buffer[i+1]) + static_cast<int>(buffer[i])*256;
-
@J.Hilk Please be aware that he is trying to convert a string containing hex numbers - not two bytes to int.
-
@jsulm Ok, fair enough, I was a bit confused by him placing the String in a QByteArray in the first place.
but thats even easier, and my code can be adjusted to that:
QString part1, part2; //Either concat them like you wanted to part1.append(part2); int value = part1.toInt(checkBool/*or nullptr*/,16); //Or convert them first to int similair to my first post int value = part1.toInt(nullptr,16)*256 + part2.toInt(nullptr,16);
-
you should really use QTextStream instead. Streams are Qt's preferred way of interacting with devices.
instead of
while (!file.atEnd()) {/* ... */ }
(P.S. no need to call split multiple times) make sure you have file opened in text mode, something likefile.open(QIODevice::ReadOnly | QIODevice::Text)
):QTextStream fileStream(&file); QString line; while (fileStream.readLineInto(&line)) { const QStringList lineParts = line.split(',',QString::KeepEmptyParts); timeStamp << lineParts.at(1); PGNList<< lineParts.at(3); DB1<< lineParts.at(9); DB2<< lineParts.at(10); const QString concatenated = DB1.last() + DB2.last(); qDebug() << concatenated << concatenated.toInt(Q_NULLPTR,16); }
-
Thank you all for your input and code snippets! I'll be studying and digesting today, thanks!!
@jsulm - I had declared DB1hex first as long, then tried as int.
@VRonin - Are QTextStream's faster? These log files can be big, running to 50-60 megabytes and 150k plus lines. I've been worried about how long it will eventually take to parse out a whole file (I've been working with a file that I cut down to 1000 lines).
Edit
@J-Hilk - if anything in my code doesn't make sense, it's 'cause I'm coding challenged :) I appreciate any input that helps me learn the right way of doing things.
Best regards
-
@MScottM said in Data Manipulation:
Are QTextStream's faster?
No. are they slower, no.
I parsed the entire Hamlet play in 3 seconds so it should not be that bad
-
@VRonin Okay - I rewrote the code to use QTextStream, and the data in the qDebug statement in your code snippet is exactly what I'm looking for (thanks!!), but when I try to put it into some kind of array that I can access from somewhere else, I'm getting errors - conversion errors, mismatch errors, ugh! I think I just don't have some syntax right.
Shouldn't the converted variable 'concatenated' (which is now 'int') be able to go into a QByteArray?
concatenated.toInt(&ok, 16); QByteArray concatData = concatenated;
Thanks for the input so far - I'm getting close to getting this figured out!
-Scott
-
@MScottM said in Data Manipulation:
Shouldn't the converted variable 'concatenated' (which is now 'int') be able to go into a QByteArray?
Yes but you have to specify the format, for example
QByteArray concatData = concatenated.toUtf8();
@MScottM said in Data Manipulation:
concatenated.toInt(&ok, 16);
toInt()
is const so unless you use the return value it does nothing. -
Okay, feeling pretty dense...sorry! I think I'm not understanding something basic. Nothing I've tried so far (trying to learn from the hints above) allows me to put the converted variable into an array for manipulating in another area of the code.
It fails with "invalid conversion from 'int' to 'const char*' [-fpermissive"]
-
YES! Two steps forward and one step back.
The code now runs and I can see the concatenated data as a string, but when I ask for the count of the arrays, PGNList gives me 1000 which is equal to the number of lines in the log file (and what I expect), but the QByteArray concatData says 4. I'm guessing that is the length of the concatenated string going into it (like CDAB)?
So do I have to define the size of the array element somehow? Just guessing, anyway, I'm further along than I was a week ago!
-
I finally figured it out - thanks to all for the hints and tips.
Now on to the part that I was really trying to get to: displaying the data in a useful way. I think I'm going to try to graph this particular data (heading) with heading on the Y axis and a timestamp on the X.
Best regards!