PDA

View Full Version : Searching for a string in QByteArray



nagabathula
23rd November 2010, 10:02
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.

QByteArray data = (QByteArray::fromRawData(buff,numBytes)).toHex();
QByteArray edtdata(data);
QByteArray serstr("1A2C11000000");
edtdata.indexOf(serstr);
for(int i=0; i <edtdata.size(); i++)
{
newdat.append(edtdata.at(k));
}
textBrowser->append(newdat);

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. ..?


1A2C1100000d110a110a110a110a110a110a110a110a110a11 0a110a110a11
1A2C1100000e110a110a110a110a110a110a110a110a110a11 0a110a110a110a110a11
1A2C1100000f110a110a110a110a110a110a110a110a110a11 0a110a110a110a110a11

1A2C11000000 110a110a110a110a110a110a110a110a110a110a110a110a11 0a110a11
1A2C11000001110a110a110a110a110a110a110a110a110a11 0a110a110a110a110a11
1A2C11000002110a110a110a110a110a110a110a110a110a11 0a110a110a110a110a11
1A2C11000003110a110a110a110a110a110a110a110a110a11 0a110a110a110a110
does anyone have a idea of it. ?
thank you

wysota
23rd November 2010, 10:05
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.

nagabathula
23rd November 2010, 10:21
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. :confused:

wysota
23rd November 2010, 13:44
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?

nagabathula
23rd November 2010, 16:17
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,.??


QByteArray newdat;
int k=0;
QByteArray data = (QByteArray::fromRawData(buff,numBytes)).toHex();
for (int i = 0; i < data.size(); i++)
{
k++;
// 1A2C11000000 Search string start of the frame
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)
{
// QMessageBox::information(this, "Test","String found at" + QString::number(k) + " Position");
da.append(data.toHex());
newdat.append(da.at(k)); // k = position of the string 1A2C11000000
textBrowser->append(newdat);
}
}
this is what i am trying to do so that i can append the data from the string found position..

wysota
23rd November 2010, 16:39
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.

nagabathula
23rd November 2010, 16:49
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

wysota
23rd November 2010, 20:30
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:

QByteArray pattern("1A2C11000000");
And this is a binary pattern:

QByteArray pattern("\x1A\x2C\x11\x00\x00\x00");
Now use QByteArrayMatcher to find your byte array.

QByteArrayMatcher matcher(pattern);
int pos = 0;
while((pos = matcher.indexIn(data, pos)) != -1) {
qDebug() << "pattern found at pos" << pos;
}

Note, you have to care about a situation when a chunk of data ends in the middle of the pattern.

nagabathula
24th November 2010, 05:35
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.

wysota
24th November 2010, 08:40
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.

nagabathula
24th November 2010, 09:28
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. ?


QByteArrayMatcher matcher(pattern);
int pos = 0;
while((pos = matcher.indexIn(data, pos)) != -1) {
// qDebug() << "pattern found at pos" << pos;
pos = i;
newdat.append(data.at(i)); // trying to append data from the string found position
textBrowser->append(newdat);
}
but this does't work right .!!

thank you

wysota
24th November 2010, 10:05
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.

nagabathula
24th November 2010, 10:50
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;


QByteArray data = (QByteArray::fromRawData(buff,numBytes)).toHex();

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.


QByteArrayMatcher matcher(pattern);
int pos = 0;
while((pos = matcher.indexIn(data, pos)) != -1) {
QMessageBox::information(this, "Start of frame found at" + QString::number(pos) + " Position");
}
i hope you understood what i am trying to do.

Thank you

high_flyer
24th November 2010, 10:55
how should i append all data from this position in ByteArray.
QByteArray::append() ?

wysota
24th November 2010, 10:57
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.



QByteArray data = (QByteArray::fromRawData(buff,numBytes)).toHex();
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.

high_flyer
24th November 2010, 11:12
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?

nagabathula
24th November 2010, 11:39
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.

if(port->bytesAvailable())
{
numBytes = port->bytesAvailable();
if(numBytes > sizeof(buff))
numBytes = sizeof(buff);
i = port->read(buff,numBytes);
QByteArray data = (QByteArray::fromRawData(buff,numBytes)).toHex();