Results 1 to 17 of 17

Thread: Searching for a string in QByteArray

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 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.


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

    nagabathula (24th November 2010)

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.