PDA

View Full Version : QByteArray data manuplation.



nagabathula
1st December 2010, 16:14
hello frenz, i am having a small problem i am not able to figure out how to remove first 12 Chars from a QByteArray and append the next 40 chars into another byte array or string.
something like i have to remove the first 6 bytes 03b702200001 and then append the next 20 bytes 00de00de00de00de00de00de00de00de00de00de00de00de00 de00de00de00de00de00de00de00de and so on to the next line of data also i tried using .remove(); but i cud't get it right.


03b702200001 00de00de00de00de00de00de00de00de00de00de00de00de00 de00de00de00de00de00de00de00de
03b702200002 00de00de00de00de00de00de00de00de00de00de00de00de00 de00de00de00de00de00de00de00de
03b702200003 00de00de00de00de00de00de00de00de00de00de00de00de00 de00de00de00de00de00de00de00de
03b702200004 00 de00de00de00de00de00de00de00de00de00de00de00de00de 00de00de00de00de00de00de00de

i am getting continuous data from rs232 terminal which i am pushing to a QByteArray this is what i did to remove the first 12 characters and append the next 40 characters .

for(int i=12; i< datstr.size(); i++)
{
newdat.append(datstr.at(i));
bytecount++

if(bytecount==40)
{
newdat.remove(0,12);
bytecount=0;
}
}
textBrowser->append(newdat);
datstr.clear();
i am not able to think about a exact logic to do this. :(

wysota
1st December 2010, 16:51
Putting binary data into a text edit box is not going to end up with anything useful so either your data is textual and not binary or you are going to get weird result in the text browser. One way or the other you do not seem to control what you are doing.

ChrisW67
2nd December 2010, 00:46
What's wrong with QByteArray::mid() or QString::mid()?

I think you are confusing yourself by talking about a series of bytes in an array one moment and the representation of those bytes as a string in hexadecimal the next. They are different beasts and require different handling.

If the data is raw bytes in a QByteArray then you want 20 bytes starting a byte 6. If the data is a hex string in either a QByteArray or a QString then you want 40 characters starting at character 12.

As an aside, each of your example hexadecimal strings represents 46 bytes of raw data as 92 hex digits (spaces ignored). You seem to indicate you are expecting to get all the bytes other than the first 6 but are asking for only 20.

nagabathula
3rd December 2010, 16:39
Hello thank you for replying,
i am actually pushingback that data from the sqlite db where i have saved all the data after aligning and bitshifting.. Now i need to remove the frame id in order to save only the channel data. the frame id is 6 bytes in size 03b702200001 after which 20 bytes is data after which is 6 bytes of frame id again i have to remove those 6 bytes and save the next 20 bytes this cycle goes on, i could't use QStringPattern to remove the frameID cause the frame id increments after every 20 bytes so the string patter would change. i need to think about a logic to remove 6 bytes and save 20 bytes remove the next 6 and save 20 bytes again and so on.. Which i am not able to figure out i tried looking for a similar example but din find.:confused:
I actually though evey thing wud be in Bytes in QByteArray but its taking for example "1d" as 2 chars i think then a single byte. thats why i got a lil confused with it,.

@wysota I am appending the data into a text Browser to just check the out put. i would be removing the text Browser later and save all that data into a db. tats all temperature data i will equate the data and display it in QTableView.

thank you

wysota
3rd December 2010, 20:22
@wysota I am appending the data into a text Browser to just check the out put. i would be removing the text Browser later and save all that data into a db. tats all temperature data i will equate the data and display it in QTableView.
That's not the point. You can't append binary data to a widget that expects text so you either don't have text or you don't have binary, there is no automatic conversion between the two. One of your two usages of the data is bound to fail.

ChrisW67
3rd December 2010, 22:11
Hello thank you for replying,
i am actually pushingback that data from the sqlite db where i have saved all the data after aligning and bitshifting.. Now i need to remove the frame id in order to save only the channel data. the frame id is 6 bytes in size 03b702200001 after which 20 bytes is data after which is 6 bytes of frame id again
No it isn't if your example data is anything to go by. Expressed as a hexadecimal string with bytes numbered:


// 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3
// 0 1 2 3 4 5 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
-- 03b702200001 00de00de00de00de00de00de00de00de00de00de00de00de00 de00de00de00de00de00de00de00de
-- 03b702200002 00de00de00de00de00de00de00de00de00de00de00de00de00 de00de00de00de00de00de00de00de
There are 40 bytes after the 6-byte header.


i have to remove those 6 bytes and save the next 20 bytes this cycle goes on, i could't use QStringPattern to remove the frameID cause the frame id increments after every 20 bytes so the string patter would change.
Nobody suggested you use "QStringPattern", primarily because it does not exist in Qt.


i need to think about a logic to remove 6 bytes and save 20 bytes remove the next 6 and save 20 bytes again and so on.. Which i am not able to figure out i tried looking for a similar example but din find.:confused:
You certainly do need to think about the logic. You don't need to remove anything in order to access a subsection from the middle.

I actually though evey thing wud be in Bytes in QByteArray but its taking for example "1d" as 2 chars i think then a single byte. thats why i got a lil confused with it,.
Everything in a QByteArray is a byte. The string "1d" is two characters, and in a QByteArray each ASCII character is represented by a byte. You are going to really struggle with this low-level byte manipulation until you grasp the difference between the value in a byte (a binary number) and the representation of that value in human-readable form (whether hexadecimal, octal, decimal, or base64). This is not the place to learn that.

Answer these questions for yourself:
What are you working on: an array of the actual bytes, or a hexadecimal string representing the bytes?
What are you trying to obtain: an array of the actual desired bytes or a hexadecimal string representing the bytes?
How many bytes, or hexadecimal characters if using a string, are there?
How can the functions already pointed out in this thread access parts of the byte array (or string)?

nagabathula
4th December 2010, 15:08
Hello ChrisW i solved my problem. this is what i did to remove the frame id and append only the required data. I checked the final result on a textBrowser it shows exactly what i expected.



for(int i=0; i< datstr.size(); i++)
{
newdat.append(datstr.mid(posi, 80)); //intial value of posi =12;
posi+=92;
}
textBrowser_2->append(newdat);

Thank you so much you made me think and solve my problem myself i read about QByteArray::mid as you suggested me before in this thread, and came up with this one.

Thank you again