PDA

View Full Version : easiest Way QString can do



baray98
15th April 2008, 01:41
Guys,

I am looking for the easiest way the QString can handle this.. say I have a QString of


QString sample = "00223344556677992200";

I want to split them like below:


QString 1 = 00
QString 2 = 2233
QString 3 = 44
QString 4 = 5566779922
QString 5 = 00

baray98

MarkoSan
15th April 2008, 02:05
Guys,

I am looking for the easiest way the QString can handle this.. say I have a QString of

QString sample = "00223344556677992200";

I want to split them like below:

QString 1 = 00
QString 2 = 2233
QString 3 = 44
QString 4 = 5566779922
QString 5 = 00

baray98

D6 you want to split every stringg with this pattern (first string 2 chars, second 4 chars, ....)?

codeslicer
15th April 2008, 03:27
Did you look at the QString documentation? There are plenty of String-modifying and extracting functions.

QString::split() (http://doc.trolltech.com/latest/qstring.html#split) - this returns a QStringList which you can later query.

or QString::section() (http://doc.trolltech.com/latest/qstring.html#section)

Just have a look there...

baray98
15th April 2008, 08:08
actually i am trying to decode an intel hex file and I want the smallest line of code given all the QString stuff and Yes i have read the documentation but it seeems like theres a lots of steps and i was just wondering if you guys have some other ideas(which is very efficient).

baray98

aamer4yu
15th April 2008, 08:39
Does the sample u gave handle all cases ??
Whats the format of hex file u are trying to read.
Apart from QString, QRegularExpression might be of help to you,

baray98
15th April 2008, 08:45
here is the INTEL HEX FILE definition



An Intel HEX file is composed of any number of HEX records. Each record is made up of five fields that are arranged in the following format:

:llaaaatt[dd...]cc
Each group of letters corresponds to a different field, and each letter represents a single hexadecimal digit. Each field is composed of at least two hexadecimal digits-which make up a byte-as described below:

: is the colon that starts every Intel HEX record.
ll is the record-length field that represents the number of data bytes (dd) in the record.
aaaa is the address field that represents the starting address for subsequent data in the record.
tt is the field that represents the HEX record type, which may be one of the following:
00 - data record
01 - end-of-file record
02 - extended segment address record
04 - extended linear address record
dd is a data field that represents one byte of data. A record may have multiple data bytes. The number of data bytes in the record must match the number specified by the ll field.
cc is the checksum field that represents the checksum of the record. The checksum is calculated by summing the values of all hexadecimal digit pairs in the record modulo 256 and taking the two's complement.



i think this can be done easily with standard c++ fscanf but I I was wondering if QString with QTextDataStream can handle it better...

baray98

wysota
15th April 2008, 08:58
Using QRegExp:

QRegExp rx(":([0-9A-F]{2})([0-9A-F]{4})([0-9A-F]{2})([0-9A-F]*)([0-9A-F]{2})");
QString str = "...";
QStringList fields;
if(rx.exactMatch(str)) fields = rx.capturedTexts();

MarkoSan
15th April 2008, 10:01
What about QByteArray? While reading file you can fill the instance of it ...

baray98
15th April 2008, 20:00
QRegExp is good ,i am not too familiar with it (need some work on my part ) . If you guys can lead me the way on how to break
dd is a data field that represents one byte of data. into pairs using QRegExp I will highly appreciate it

reading the tricks of QRegExp,

baray98

wysota
15th April 2008, 20:33
What do you want to break here? If you have to characters in a string that represents a hexadecimal number, use QString::toInt() and you're done.

baray98
15th April 2008, 21:30
I am trying to break the data line into bytes

fields.at(3) // data of the hex line
from the code below



QRegExp rx(":([0-9A-F]{2})([0-9A-F]{4})([0-9A-F]{2})([0-9A-F]*)([0-9A-F]{2})");QString str = "...";QStringList fields;if(rx.exactMatch(str)) fields = rx.capturedTexts();


baray98

baray98
15th April 2008, 21:44
QRegExp rx(":([0-9A-F]{2})([0-9A-F]{4})([0-9A-F]{2})([0-9A-F]*)([0-9A-F]{2})");
QString str = "...";
QStringList fields;
if(rx.exactMatch(str)) fields = rx.capturedTexts();


sorry i mess up my cut and paste in the prior reply

baray98

wysota
15th April 2008, 21:49
QString str = fields.at(3);
for(int i=0;i<str.size();i+=2)
qDebug() << str.mid(i,2).toInt(0, 16);