concatenate two QStrings
-
Hello, i have new problem.I have read a lot of threads but all the suggestions didn't work up to now.
I have a hex file with "10 F9 F8 05" I want to combine " 10 F9" to "10F9"
here is part of my code with outcommented tries.I have always "1000"case 1 : high.append(hexMatch.capturedRef(0)); qDebug()<<"high=="<<high;//=10 case 2: { low.append(hexMatch.capturedRef(0)); qDebug()<<"low=="<<low; //=F9 highlow=high ; highlow += low; // highlow=*high + *low; // highlow.append(low); // highlow=QString("%1 %2").arg(high).arg(low); qDebug()<<"highlow=="<<highlow; // should be 10F9 is 1000 y1.append(highlow.toUInt(Q_NULLPTR,16)); qDebug()<<"y1"<<y1;}break;
who can help?
-
Hi
What types are low and high ? -
Ok
is the input always just "10 F9 F8 05"
or more like
10 F9 F8 0510 F9 F8 0510 F9 F8 0510 F9 F8 0510 F9 F8 0510 F9 F8 05
? -
it is the long type.My code was ok up to now. I had read each value as single .But I need to concatenate two values to one and then transver it to dec.
Meanwhile I changed my code this wayhigh="";low=""; switch(h) { case 1 : high +=hexMatch.capturedRef(0); qDebug()<<"high=="<<high;//=10 case 2: { low +=(hexMatch.capturedRef(0)); qDebug()<<"low=="<<low; //=F9 ``but then it recognices only the low value.`
-
@Leopold
I really don't understand what you are saying!
Twice in a row you are using the samehexMatch.capturedRef(0);
;hexmatch
does not change, and they are both usingcapturedRef(0)
. You say they give different results (10
andF9
), but as it stands it does not look like they can.... So what is your actual code if it's not what you show? -
Hi JonB
this is my actual codeif (line_count>3) // read line from line 4 { x.append(tick); // this will be x-line in a plot tick++; int h=0; const QRegularExpression eingangExp(QStringLiteral("^\\s*([0-9a-fA-F]{2}):")); const QRegularExpression hexRegExp(QStringLiteral("[0-9a-fA-F]{2}(?!:)")); const QRegularExpressionMatch eingangMatch = eingangExp.match(line); const QRegularExpressionMatch hexMatch; if(eingangMatch.hasMatch()) { if(eingangMatch.capturedRef(1).compare("80",Qt::CaseInsensitive)==0) { // Eingang==("80:") qDebug() << eingangMatch; for(QRegularExpressionMatchIterator i = hexRegExp.globalMatch(line); i.hasNext(); ++h) { //qDebug()<< "h="<<h; const QRegularExpressionMatch hexMatch = i.next(); qDebug() << hexMatch.capturedRef(0).toUInt(Q_NULLPTR,16); switch(h) { case 1 : high=""; high.append(hexMatch.capturedRef(0)); qDebug()<<"high=="<<high; case 2: {low=""; low.append(hexMatch.capturedRef(0)); qDebug()<<"low=="<<low; highlow=QString("%1%2").arg(high).arg(low); qDebug()<<"high2=="<<highlow; // should be 10F9 is 1000 y2.append(highlow.toUInt(Q_NULLPTR,16)); qDebug()<<"y1"<<y1;}break; // case 1 : y56.append(hexMatch.capturedRef(0).toUInt(Q_NULLPTR,16)); break;//Dataframe // case 2 : y1.append(hexMatch.capturedRef(0).toUInt(Q_NULLPTR,16)); break;//engine speed // y54.append(hexMatch.capturedRef(0).toUInt(Q_NULLPTR,16)); break;//engine speed case 3 : { qreal t=hexMatch.capturedRef(0).toUInt(Q_NULLPTR,16);//coolant temp qreal u=(t-32)*55556/100000;// calculate celsius from fahrenheit y2.append(u); break;}//engine speed raw case 4 : {qreal t=hexMatch.capturedRef(0).toUInt(Q_NULLPTR,16);//coolant temp qreal u=(t-32)*55556/100000; y3.append(u); break;}//water temp case 5 : {qreal t=hexMatch.capturedRef(0).toUInt(Q_NULLPTR,16);//coolant temp qreal u=(t-32)*55556/100000; y4.append(u); break;}//ambienttemp case 6 : {qreal t=hexMatch.capturedRef(0).toUInt(Q_NULLPTR,16);//coolant temp qreal u=(t-32)*55556/100000; y5.append(u); break;}//intake air case 7 : y6.append(hexMatch.capturedRef(0).toUInt(Q_NULLPTR,16)); break;//fuel temp case 8 : y7.append(hexMatch.capturedRef(0).toUInt(Q_NULLPTR,16)); break;//Map
and you are right , low is filled with the value from case 1 too.
This is the line to be read:"80: 1C 00 9F 41 FF 43 41 5C 71 2B 00 and so on -
and you are right , low is filled with the value from case 1 too.
It's very naughty to tell us that
qDebug()
/your code shows you something, and when I challenge you you now say it returns something else! :)I don't claim to understand your whole code or your reg exp, but:
"[0-9a-fA-F]{2}(?!:)" ... hexMatch.capturedRef(0)
Capture 0 is the match for the first
( ... )
in your reg exp. For you, that's the(?!:)
, whatever that is. But don't you want the two hex digits from the reg exp? So something more like:"([0-9a-fA-F]{2})(?!:)"
(I have inserted parentheses around the two digits match)?
-
@Leopold
OK, forget what I said earlier . Here are your (two) problems:-
So far as I can spot: Your
h
starts off as0
. You only increment it at the end of each time through the loop. So e.g. first time it does not match any of thecase
clauses. What's going on with that? -
You do not have a
break;
statement at the end ofcase 1:
. So it continues straight into thecase 2:
. With exactly the samehexMatch.capturedRef(0)
value. What is going on with that?
Sort your code out sensibly and have another go!
-
-
hi JonB and mrJJ
now it is working i had two mistakes.
First : the break was missing after case 1
second: had the wrong y ```
case 1 : high="";
high.append(hexMatch.capturedRef(0));
qDebug()<<"high=="<<high;break;// this break was missing
case 2: {low="";
low.append(hexMatch.capturedRef(0));
qDebug()<<"low=="<<low;
highlow=QString("%1%2").arg(high).arg(low);
qDebug()<<"high2=="<<highlow;
y1.append(highlow.toUInt(Q_NULLPTR,16));//this y was wrong, former y2
qDebug()<<"y1"<<y1;}break;thank you for your help.
-
@JonB
Hi JonB
you gave answer at thesame time when I did.
I realized the break too. Sometimes the code is too near to the eyes. :-)
With the h, it's by intention.
This is the line to be read in
This is the line to be read:"80: 1C 00 9F 41 FF 43 41 5C 71 2B 00 and so on
and I want to start it after the 1C.
Thank you again.