Results 1 to 13 of 13

Thread: easiest Way QString can do

  1. #1
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default easiest Way QString can do

    Guys,

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

    Qt Code:
    1. QString sample = "00223344556677992200";
    To copy to clipboard, switch view to plain text mode 

    I want to split them like below:

    QString 1 = 00
    QString 2 = 2233
    QString 3 = 44
    QString 4 = 5566779922
    QString 5 = 00
    baray98
    Last edited by marcel; 8th August 2008 at 00:44. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: easiest Way QString can do

    Quote Originally Posted by baray98 View Post
    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, ....)?
    Qt 5.3 Opensource & Creator 3.1.2

  3. #3
    Join Date
    Feb 2008
    Posts
    153
    Thanks
    40
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Lightbulb Re: easiest Way QString can do

    Did you look at the QString documentation? There are plenty of String-modifying and extracting functions.

    QString::split() - this returns a QStringList which you can later query.

    or QString::section()

    Just have a look there...

  4. #4
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: easiest Way QString can do

    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

  5. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: easiest Way QString can do

    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,

  6. #6
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: easiest Way QString can do

    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

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: easiest Way QString can do

    Using QRegExp:
    Qt Code:
    1. QRegExp rx(":([0-9A-F]{2})([0-9A-F]{4})([0-9A-F]{2})([0-9A-F]*)([0-9A-F]{2})");
    2. QString str = "...";
    3. QStringList fields;
    4. if(rx.exactMatch(str)) fields = rx.capturedTexts();
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to wysota for this useful post:

    baray98 (15th April 2008)

  9. #8
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: easiest Way QString can do

    What about QByteArray? While reading file you can fill the instance of it ...
    Qt 5.3 Opensource & Creator 3.1.2

  10. #9
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: easiest Way QString can do

    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

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: easiest Way QString can do

    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.

  12. #11
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: easiest Way QString can do

    I am trying to break the data line into bytes
    Qt Code:
    1. fields.at(3) // data of the hex line
    To copy to clipboard, switch view to plain text mode 
    from the code below

    Qt Code:
    1. 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();
    To copy to clipboard, switch view to plain text mode 

    baray98

  13. #12
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: easiest Way QString can do

    Qt Code:
    1. QRegExp rx(":([0-9A-F]{2})([0-9A-F]{4})([0-9A-F]{2})([0-9A-F]*)([0-9A-F]{2})");
    2. QString str = "...";
    3. QStringList fields;
    4. if(rx.exactMatch(str)) fields = rx.capturedTexts();
    To copy to clipboard, switch view to plain text mode 

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

    baray98

  14. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: easiest Way QString can do

    Qt Code:
    1. QString str = fields.at(3);
    2. for(int i=0;i<str.size();i+=2)
    3. qDebug() << str.mid(i,2).toInt(0, 16);
    To copy to clipboard, switch view to plain text mode 

  15. The following user says thank you to wysota for this useful post:

    baray98 (15th April 2008)

Similar Threads

  1. QString static callback function from CURL
    By tpf80 in forum Qt Programming
    Replies: 12
    Last Post: 16th May 2007, 20:47
  2. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59
  3. Converting QString to unsigned char
    By salston in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 22:10
  4. Replies: 2
    Last Post: 10th February 2006, 12:57
  5. [SOLVED] Widget plugin ... how to ?
    By yellowmat in forum Newbie
    Replies: 10
    Last Post: 29th January 2006, 20:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.