Results 1 to 17 of 17

Thread: Searching for a string in QByteArray

  1. #1
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Searching for a string in QByteArray

    Hello frenz i had a doubt in searching a string in QByteArray, i wanted to actually find the start of the frame "1A2C110000" and then append data from there all the data before the start of the frame to be discarded.. i think the only the index position of the first search string is taken so i am not getting the right out put.
    Qt Code:
    1. QByteArray data = (QByteArray::fromRawData(buff,numBytes)).toHex();
    2. QByteArray edtdata(data);
    3. QByteArray serstr("1A2C11000000");
    4. edtdata.indexOf(serstr);
    5. for(int i=0; i <edtdata.size(); i++)
    6. {
    7. newdat.append(edtdata.at(k));
    8. }
    9. textBrowser->append(newdat);
    To copy to clipboard, switch view to plain text mode 

    this is a part of the data i am getting through rs232 terminal. .. i need to search for the start of the frame 1A2C11000000 and append the from there all the data before should be discarded. . i don't know exactly go about it. ..?

    Qt Code:
    1. 1A2C1100000d110a110a110a110a110a110a110a110a110a110a110a110a11
    2. 1A2C1100000e110a110a110a110a110a110a110a110a110a110a110a110a110a110a11
    3. 1A2C1100000f110a110a110a110a110a110a110a110a110a110a110a110a110a110a11
    4.  
    5. 1A2C11000000 110a110a110a110a110a110a110a110a110a110a110a110a110a110a11
    6. 1A2C11000001110a110a110a110a110a110a110a110a110a110a110a110a110a110a11
    7. 1A2C11000002110a110a110a110a110a110a110a110a110a110a110a110a110a110a11
    8. 1A2C11000003110a110a110a110a110a110a110a110a110a110a110a110a110a110
    To copy to clipboard, switch view to plain text mode 
    does anyone have a idea of it. ?
    thank you

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

    Default Re: Searching for a string in QByteArray

    My guess is you are searching for a string right now and not for binary data. Build a binary (hexadecimal) pattern and search for that.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    nagabathula (23rd November 2010)

  4. #3
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: Searching for a string in QByteArray

    hello wysota i am searching for a Hexadecimal string only and not binary data. .. can you please show we a example of how we can build a hexadecimal pattern and search.. I looked so much but din't find any thing about search in the forum.

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

    Default Re: Searching for a string in QByteArray

    So if you open the file with a text editor (say... Notepad or whatever you are using) you see exactly the text you posted? Is this textual or binary data?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: Searching for a string in QByteArray

    Hello wysota it is actually in acii format i am converting it into Hexadecimal when i retrieve it from the rs232 terminal, I have figured out a logic to search a string in a QByteArray but i am stuck at the last part now. But i am getting an error i am able to find out the position where the required String is but how do i pass it as a the index position to append data from there. Can some one pls tell me how i pass the k value to the QByteArray index position,.??

    Qt Code:
    1. QByteArray newdat;
    2. int k=0;
    3. QByteArray data = (QByteArray::fromRawData(buff,numBytes)).toHex();
    4. for (int i = 0; i < data.size(); i++)
    5. {
    6. k++;
    7. // 1A2C11000000 Search string start of the frame
    8. if(data.at(i) == 1A|| data.at(i+1) == 2C||data.at(i+2) ==11||data.at(i+3) ==00||data.at(i+4) ==00||data.at(i+5) ==00)
    9. {
    10. // QMessageBox::information(this, "Test","String found at" + QString::number(k) + " Position");
    11. da.append(data.toHex());
    12. newdat.append(da.at(k)); // k = position of the string 1A2C11000000
    13. textBrowser->append(newdat);
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 
    this is what i am trying to do so that i can append the data from the string found position..

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

    Default Re: Searching for a string in QByteArray

    Quote Originally Posted by nagabathula View Post
    Hello wysota it is actually in acii format i am converting it into Hexadecimal
    No, you are converting it from hexadecimal format. "AF" is not hexadecimal, '\xAF' is.

    when i retrieve it from the rs232 terminal
    Why are you converting it to ascii? You are immediately doubling (and quadrupling when you use QString) the space required to hold the data at the same time vastly decreasing processing speed.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: Searching for a string in QByteArray

    Hello wysota, sorry i just spoke to the embedded group, they are sending the data in binary format not asii.. i am converting it from binary to Hexadecimal... How can i pass the value of k as the index position for the QByteArray data; so that i can append only from the start of the frame.

    Thank You
    Last edited by nagabathula; 23rd November 2010 at 17:26.

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

    Default Re: Searching for a string in QByteArray

    Quote Originally Posted by nagabathula View Post
    i am converting it from binary to Hexadecimal
    No, you are not. Binary data is already hexadecimal. It's like you said "I'm converting my sentences from letters to words".

    How can i pass the value of k as the index position for the QByteArray data; so that i can append only from the start of the frame.
    Let me solve this one for you but promise you will learn what "hexadecimal", "binary" and "ascii" really means.

    From what I understand you are trying to find some data beginning with 0x1A2C11000000. To do that you need to construct such a pattern - a binary pattern, not a textual one.

    So here goes...
    this is a text pattern:
    Qt Code:
    1. QByteArray pattern("1A2C11000000");
    To copy to clipboard, switch view to plain text mode 
    And this is a binary pattern:
    Qt Code:
    1. QByteArray pattern("\x1A\x2C\x11\x00\x00\x00");
    To copy to clipboard, switch view to plain text mode 
    Now use QByteArrayMatcher to find your byte array.
    Qt Code:
    1. QByteArrayMatcher matcher(pattern);
    2. int pos = 0;
    3. while((pos = matcher.indexIn(data, pos)) != -1) {
    4. qDebug() << "pattern found at pos" << pos;
    5. }
    To copy to clipboard, switch view to plain text mode 

    Note, you have to care about a situation when a chunk of data ends in the middle of the pattern.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    nagabathula (24th November 2010)

  11. #9
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: Searching for a string in QByteArray

    Hello wysota thank you for the reply you explained me the whole concept very well. Now i have a understanding of what hex , ascii , binary is. .

    can you please tell me why you put the while wysota i din understand it.

    while((pos = matcher.indexIn(data, pos)) != -1)
    how do i pass the pos value to data index wysota so that i append data from there.

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

    Default Re: Searching for a string in QByteArray

    Quote Originally Posted by nagabathula View Post
    can you please tell me why you put the while wysota i din understand it.
    No, I can't. This is basic C++, google for it.

    how do i pass the pos value to data index wysota so that i append data from there.
    I have no idea what you mean. You can do whatever you want with the value of pos.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #11
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: Searching for a string in QByteArray

    Hello wysota from the below code the index position of the search pattern is found right. .. The index position of the string is present in "pos". now how do i pass tat index position so that i can append data from that position from the QByteArray data; to a textbrowser or a db. ?

    Qt Code:
    1. QByteArrayMatcher matcher(pattern);
    2. int pos = 0;
    3. while((pos = matcher.indexIn(data, pos)) != -1) {
    4. // qDebug() << "pattern found at pos" << pos;
    5. pos = i;
    6. newdat.append(data.at(i)); // trying to append data from the string found position
    7. textBrowser->append(newdat);
    8. }
    To copy to clipboard, switch view to plain text mode 
    but this does't work right .!!

    thank you

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

    Default Re: Searching for a string in QByteArray

    Overwriting the position of the start of the pattern right after you have found it is probably not a very bright idea. You either don't know what you are doing or your algorithm for doing "that" (whatever "that" is) is flawed.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. #13
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Question Re: Searching for a string in QByteArray

    This is what i am trying to do, I am getting some known data from rs232 terminal, i have to find the start of the frame "1A2C11000000" all the data after this frame id is the channel data.. I just need to find the start of the frame in order to start saving the channel data from there.. I am storing the data to the QByteArray data;

    Qt Code:
    1. QByteArray data = (QByteArray::fromRawData(buff,numBytes)).toHex();
    To copy to clipboard, switch view to plain text mode 

    As you suggested me this code before.Here the index position of the pattern in the QByteArray data is in pos. that is like a reference point for me that it is the start of the frame. how should i append all data from this position in ByteArray.

    Qt Code:
    1. QByteArrayMatcher matcher(pattern);
    2. int pos = 0;
    3. while((pos = matcher.indexIn(data, pos)) != -1) {
    4. QMessageBox::information(this, "Start of frame found at" + QString::number(pos) + " Position");
    5. }
    To copy to clipboard, switch view to plain text mode 
    i hope you understood what i am trying to do.

    Thank you

  16. #14
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Searching for a string in QByteArray

    how should i append all data from this position in ByteArray.
    QByteArray::append() ?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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

    Default Re: Searching for a string in QByteArray

    Quote Originally Posted by nagabathula View Post
    I just need to find the start of the frame in order to start saving the channel data from there..
    I have already written that part for you.

    Qt Code:
    1. QByteArray data = (QByteArray::fromRawData(buff,numBytes)).toHex();
    To copy to clipboard, switch view to plain text mode 
    What is "buff", what is "numBytes", why are you using toHex()?

    As you suggested me this code before.
    I didn't suggest any such code.

    Here the index position of the pattern in the QByteArray data is in pos. that is like a reference point for me that it is the start of the frame.
    And how do you intend to find the end of the frame?

    Go back to post #8 in this thread, read the last sentence and make sure you understand it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  18. #16
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Searching for a string in QByteArray

    sorry i just spoke to the embedded group, they are sending the data in binary format not asii.. i am converting it from binary to Hexadecimal...
    Sorry for off topic post but I can't resist:
    This looks like you are actually doing this for work.
    Is this the case?
    Are you an intern or a student doing practical work for few weeks?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  19. #17
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: Searching for a string in QByteArray

    This is what i am doing to retrieve data from rs 232 terminal. The data is continues i keep getting the data from the embbeded system.
    Qt Code:
    1. if(port->bytesAvailable())
    2. {
    3. numBytes = port->bytesAvailable();
    4. if(numBytes > sizeof(buff))
    5. numBytes = sizeof(buff);
    6. i = port->read(buff,numBytes);
    7. QByteArray data = (QByteArray::fromRawData(buff,numBytes)).toHex();
    To copy to clipboard, switch view to plain text mode 
    Last edited by nagabathula; 24th November 2010 at 15:58.

Similar Threads

  1. Replies: 4
    Last Post: 19th October 2010, 02:23
  2. Replies: 9
    Last Post: 25th July 2009, 13:27
  3. Searching in a list.
    By kaushal_gaurav in forum Qt Programming
    Replies: 4
    Last Post: 15th October 2008, 08:00
  4. Searching a QTable
    By nategoofs in forum Qt Programming
    Replies: 1
    Last Post: 8th August 2007, 22:15
  5. Convert from QByteArray to String ?
    By probine in forum Newbie
    Replies: 3
    Last Post: 25th March 2006, 15:49

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
  •  
Qt is a trademark of The Qt Company.